Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when asynchronously loaded jquery has finished loading?

I want to load jquery asynchronously on a page and then load an other script which depends on jquery (it executes jquery code when loaded). But how can I detect if jquery has finished lodading. Is there an event for this in jquery which fires when the library finished loading?

In theory I could load jquery like this:

<script async src="jquery.js" onload="jqueryloaded()"></script>

but I want the code to work on older browsers too which may not support the async and onload attributes. (In this case jquery is loaded synchronously.) That's why I'm looking for an event which jquery emits when it's loaded. Is there one?

like image 316
Tom Avatar asked Mar 06 '14 16:03

Tom


People also ask

How do I ensure jQuery is loaded?

Hence, we have used the $(document). ready() function before we can check if the plugin was loaded successfully or not. The typeof operator returns the data type of its operand in the form of a string. In this case, the operand is the jQuery $ operator itself.

Is jQuery load asynchronous?

You can use jQuery to support both synchronous and asynchronous code, with the `$.


2 Answers

You can try this:

var script = document.createElement('script');
script.src = 'jquery.js';
script.onload = jqueryloaded; // thx @Cookie_Monster
document.body.appendChild(script);
like image 194
Paul Rad Avatar answered Sep 24 '22 21:09

Paul Rad


Have you seen http://headjs.com/?. An example it's:

head.load("jQuery.js", function() {
 // Call a function when done
 console.log("Done loading jQuery");
});
like image 23
csorive Avatar answered Sep 26 '22 21:09

csorive