0

I'm trying to handle JQuery not loading.

(this is for a javascript api, frequently the developer consuming it forgets to include jquery, hence I want to instruct them on what is missing)

  1. I have a bunch of code in various files that runs on document.ready e.g.
(function ($, undefined) {
    //stuff 
})( jQuery );

If JQuery is not loaded then the code above breaks. A workaround I've found is to add a check for Jquery instead through a function e.g.

(function ($, undefined) {
   //stuff
 })( JQueryCheck() );

function JQueryCheck() {
    if (window.jQuery == undefined)
        alert("no jquery");
    else
        return jQuery;
}

I'm not sure if this is strong enough, if theres a better way to do it then let me know.

  1. On top of this I want prevent the other document.readys from running, theres no point as all depend on JQuery being loaded.

This must be common issue but I'm struggling to find a solution.

learnerplates
  • 4,257
  • 5
  • 33
  • 42
  • 5
    What sorts of problems are you having that lead to jQuery failing to load on a frequent basis? – Pointy Feb 06 '14 at 15:11
  • possible duplicate of [Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail](http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-go) – Mike Corcoran Feb 06 '14 at 15:12
  • I have a javascript API which other developers use. Frequently they forget to include jquery or other dependencies in their html. – learnerplates Feb 06 '14 at 15:12
  • This is not a common issue, I don't see how jquery will not load as long as you have included the library in your file. Just make sure jquery is included in each page. – Huangism Feb 06 '14 at 15:12
  • Load jquery through CDN, your server as backup. "Which other developers use" - if it's your server, I don't see why this wouldn't be loaded on a base level - or in other words - included in every page that loads from the template (ie index.php) – Casey Dwayne Feb 06 '14 at 15:14
  • i think he is trying to include it from a CDN and if it didn't then using native JS – Mina Gabriel Feb 06 '14 at 15:16

4 Answers4

3

Don't load script files like this <script src="some-path"></script> in Html

instead do this:

if(window.jQuery){
  $.getScript('path to js file');
}

This checks for jquery and if it exists, it loads the script and executes it.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
2

Try this:

function JQueryCheck() {
    return !!jQuery;
}

Or:

function JQueryCheck() {
    try{
        jQuery();
    } catch(e){
        if(e){
            return false;
        } else {
            return true;
        }
    }
}

Or:

function JQueryCheck() {
    if(window.jQuery){
        return true;
    }else{
        return false;
    }
}

Good luck!

Progo
  • 3,452
  • 5
  • 27
  • 44
1

Whilst it would be best to solve the issues you are having with it not loading this will do what you want it to:

if(window.jQuery) {
    //jQuery Code
}

Alternatively if you're after loading a local file if a hosted one has failed, this would work:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery.min.js"><\/script>')</script>
Styphon
  • 10,304
  • 9
  • 52
  • 86
1

Your code should work fine, but you could simplify it a bit:

(function ($, undefined) {
    if(!$) {
        return alert("no jquery");
    }

    //stuff 
})(window.jQuery);

You could also just load it when it's not loaded already:

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

        // Poll for jQuery to come into existance
        var checkReady = function(callback) {
            if (window.jQuery) {
                callback();
            }
            else {
                window.setTimeout(function() { checkReady(callback); }, 100);
            }
        };
        // Start polling...
        checkReady(main);
    } else {
        main();
    }

}());

function main() {
    alert("jquery loaded");
};

Taken from https://stackoverflow.com/a/10113434/941764

Community
  • 1
  • 1
jgillich
  • 71,459
  • 6
  • 57
  • 85
  • thanks but I believe this will break too, as the document.ready will call the jQuery variable first which does not exist at this time : })( jQuery ); – learnerplates Feb 06 '14 at 15:31
  • @learnerplates You have to move all of them into `main()`. Oh, and use [`$.ready`](http://api.jquery.com/ready/) instead. :) – jgillich Feb 06 '14 at 15:37
  • But won't })( jQuery ); blow up if JQuery is not loaded as the jQuery var does not exist at this time. It will never make it to main from what I can see. – learnerplates Feb 06 '14 at 16:39
  • 1
    I'm sorry, of course you are right, it should be `window.jQuery`. I've updated and simplified the answer a bit, it works fine now. – jgillich Feb 06 '14 at 17:48