4

I want to provide my clients a simple code to insert and get my plugin.

The code:

<div id='banner-lujanventas'></div>
<script src="http://lujanventas.com/plugins/banners/script.js" type="text/javascript"></script>

The problem is that my plugin only works with jQuery. How do I check if a version of jQuery is installed on my script.js file and if not include it? (I can only modify my /script.js file)

4 Answers 4

5

Make your own script element :

if (typeof jQuery === "undefined") {
    var script = document.createElement('script');
    script.src = 'http://code.jquery.com/jquery-latest.min.js';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
}

//edit
window.onload = function() {
    $(function(){ alert("jQuery + DOM loaded."); });
}

You must put your real onload code in a window.onload() function, and NOT in a $(document).ready() function, because jquery.js is not necessary loaded at this time.

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

4 Comments

Be careful, if jQuery isn't defined if (!jQuery) will throw an error. You should do if (typeof jQuery === "undefined")
It's including it fine but jQuery doesn't work. I added an event listener after your code $(function(){ alert("jQuery + DOM loaded."); }); and it nevers show the alert. How can I fix this?
You can't try directly this kind of code, see my EDIT for details.
If jQuery is already defined it might be a good idea to check it's version to make sure the minimum version required for your code is being used. Question is, what if it's not? Can multiple versions of jQuery be included without conflict?
2

You can check for the jQuery variable

if (typeof jQuery === 'undefined') {
    // download it
}

For downloading options, e.g. asynchronous vs. document.write, check out this article.

Comments

1

Something like this:

<script>!window.jQuery && document.write(unescape('%3Cscript src="http://yourdomain.com/js/jquery-1.6.2.min.js"%3E%3C/script%3E'))</script>

1 Comment

This is supposed to be in an external .js. I don't think this is intended for it. Or is it?
0

I dug up some old code that looks for a particular version of jQuery and loads it if it's not found plus it avoids conflicting with any existing jQuery the page already uses:

// This handles loading the correct version of jQuery without 
// interfering with any other version loaded from the parent page.
(function(window, document, version, callback) {
    var j, d;
    var loaded = false;
    if (!(j = window.jQuery) || version > j.fn.jquery || callback(j, loaded)) {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://code.jquery.com/jquery-2.1.0.min.js";
        script.onload = script.onreadystatechange = function() {
            if (!loaded && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
                callback((j = window.jQuery).noConflict(1), loaded = true);
                j(script).remove();
            }
        };
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script);
    }
})(window, document, "2.1", function($) {
    $(document).ready(function() {
        console.log("Using jQuery version: " + $.fn.jquery);

        // Your code goes here...

    });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.