I did a little experiment. Sort of:
<html> <head> <script src="head.js"></script> <script src="jquery.min.js"></script> </head> <body> <script type="text/javascript"> jQuery("document").ready(function() { console.log("ready.document"); }); </script> ... <script type="text/javascript"> head.js("a.js", "b.js", "c.js", "test.js", function() { console.log("init.scripts"); var millis = 2000; var date = new Date(); var curDate = null; do {curDate = new Date(); } while(curDate-date < millis); jQuery("#ddd").text(testTT); console.log("init.end"); }); </script> ... a lot of html content ... <div id="ddd"></div> ... <script type="text/javascript" > console.log("last line"); </script> </body>
Test.js writes to the console and defines testTT . The result is as follows.
last line init.testTT init.scripts init.end ready.document
Thus, in this simple case (using jQuery 1.8), the code in the loaded * .js is first executed, then head.js is generated for the init event, as a result, the jQuery ready event is generated. Therefore, it should be nice to wait until jQuery generates a finished event. However, if this is still a problem, you can do the following.
head.js("a.js", "b.js", "c.js", function() { jQuery("document").ready(function() { jQuery("body").trigger("your_event"); }); });
and instead of listening to jQuery("document").ready... in your * .js, start listening to the custom event that you fire jQuery("body").on("your_event", handler) .
source share