Frage

I have an area of my site that requires the end user to place a snippet of code in their own site. The snippet must make an ajax call back to my site and send some simple data.

I've set up a few code snippets in different languages to accommodate the user.

With jQuery this is very easy, both for me and for the end-user to understand and implement. With javascript, it isn't too bad either but it's not nearly as elegant.

So, I'd really prefer to just give the end-user a snippet of code that would comprise of the <link> to import jQuery and the jquery function itself.

I guess my question then is: jQuery is more compatible? Globally available? Supported? ..etc...are there any reasons left to offer up a javascript solution or will I be covering all bases with a nice clean jQuery slide?

Thanks

War es hilfreich?

Lösung 2

As you said, jQuery requires that extra import. JQuery is a file full of Javascript functions. It is an extension of the javascript library and its functions are not loaded by most major browsers, you have to include the file. If you decide to continue purely in jQuery it would be good to note this and provide instructions in the event that jQuery is not loaded on their page. It's not wrong to require libraries to run things, but it is always polite to inform people on why something may not be working for them ;)

Including something like this might be a good idea.

function myFunctionWithJQuery() {
    try {
        jQuery;
    } catch (e) {
        document.getElementById('#example-info-section').innerHTML='You need to load jQuery!';
        return;
    }
    // do something with jQuery
    return;
}

I don't exactly understand what you are doing in detail but you could call this logic before your jQuery business comes into play.

EDIT: Corrected my silliness =)

Andere Tipps

I'm not sure I understand your question, but I will try to help you towards the answer.

I "really" started developing with jQuery before JavaScript. One of the major reasons was the transitions, but also the ajax methods.

But lately, I've been using exclusively JavaScript - without jQuery, in conjunction with CSS3 for the transisions.

I find the resulting JavaScript code to be overall more readable than jQuery, and found JavaScript encourages better practices than jQuery.

One of my motivations was that it was a challenge for me to move away from jQuery and replace it with "hard" JavaScript, and also because jQuery can be a little buggy and slow sometimes.

JavaScript is easier on browsers (and CPU) and of course, faster (the jQuery library has become a relatively big file) the reduced complexity and having one layer less is always a good thing - although I have to say, jQuery is pretty stable, mature and well tested at this point.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top