0

Ideally I'd like the js file containing the plugin also look after including the jquery library.

I've played with a couple of mechanisms without success, XHR script injection and basic script injection like:

myplugin.js:

var scriptElem = document.createElement('script');
scriptElem.src = 'jquery-1.6.1.js';
document.getElementsByTagName('head')[0].appendChild(scriptElem);

(function() {
    jQuery.fn.myplugin = function() {
       ...
    }
})();

but of course jQuery won't be defined in time.

Any suggestions?

3 Answers 3

4

I'm not sure if this is the best practice, I would personally leave the jQuery include up to the developer using my plugin.

But if you insist... :)

You should use 2 files, one would be your plugin, the other would be just a loader:

myplugin_loader.js

var scriptElem;

if(!window.jQuery){
    // Include jQuery if it's not already loaded
    scriptElem = document.createElement('script');
    scriptElem.src = 'jquery-1.6.1.js';
    scriptElem.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(scriptElem);
}

// Include the main plugin after jQuery
scriptElem = document.createElement('script');
scriptElem.src = 'myplugin.js';
scriptElem.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(scriptElem);

myplugin.js

(function() {
    jQuery.fn.myplugin = function() {
       ...
    }
})();

If jQuery is surely loaded, you can use myplugin.js, if in doubt, include myplugin_loader.js. Although as I said I would recommend against it.

Sign up to request clarification or add additional context in comments.

4 Comments

I typically think if something is a bad practice then I advise against it and explain why. I try not to give advise that could be considered a "bad practice"
@mcgrailm - sometimes you just have to hold your nose and make it work. If you know WHY something is a bad practise, then you should explain what the issues are, when they apply and if and how they can be mitigated - then the coder can make an educated risk assessment. If we all refused to follow anything other than best-practises I'm pretty sure many of us would be out of jobs before too long! In my experience management tends to be quite impatient!
That's correct BonyT. There are many outside factors at play here and a so called bad practice can often become the best option.
I had a play Darth but the same problem will exist, i.e. the asynchronous nature of appendChild will have myplugin.js starting to be parsed before the larger jquery framework has finished being parsed. Static loading of scripts appears to load in parallel but execute synchronously. I can't visualise how to mimic that behavior dynamically at this stage. Synchronous XHR I'm thinking would also block loading, a penalty which I don't wish, nor do I wish to put a callback in jquery.js or start polling.
3

you should not include jquery in your plugin jQuery is a requirement or dependency to use the plugin. You should Never include it in your plugin ! If you included it this would force the user to use a specific version of jquery and may even cause problems .

6 Comments

It would not be included in the file containing the plugin, not within the plugin code itself.
On the contrary I wish to force the user to use a specific version of jquery, the version that my plugin has been tested against. If the client already uses an old version of jquery, I do not wish them to have to upgrade their jquery to use my plugin. I will handle the conflict between multiple versions of jquery.
so you think you can anticipate what problems are going to occur in future releases of jQuery that your users my include ?
Can you please rephrase, I'm not getting what you're saying?
Let say user includes jquery 2.0 how are you going to anticipate what problems would cone from loading that
|
0

Copy the jquery library into the top of the same file as the plug-in?

Note that if you took this (not recommended approach), you should use noConflict and assign some personal variable to the jquery reference eg.

 var $myjQuery = jQuery.noConflict();

and use this reference in your plugin instead of just $ or jQuery

1 Comment

Actually I forgot to indicate that I need the jquery library separate (for ease of integrity checking).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.