4 * Help to async load jQuery and supporting inline javascript with calls like
7 * $(document).ready(function(){})
9 * Include it, then just call :
10 * jQl.loadjQ('//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
12 * You can also use it to load jQuery-dependent module in parallel,
13 * it will be queue and run after jQuery is loaded :
14 * jQl.loadjQ('//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
15 * jQl.loadjQdep('my.example.com/js/myplugin.jquery.js');
17 * If you use a defer inline script, you have to manually call boot() function :
19 * <script defer type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
20 * <script type="text/javascript">jQl.boot();</script>
24 * jQuery will be loaded without blocking browser rendering,
25 * and during this all inline calls to $(document).ready() and $.getScript()
27 * As soon as jQuery is ready, all queued ready() and getScript() calls will be run
28 * respecting their initial order
31 * At the moment, inline call and getScript executions are not waiting jQuery-dependent modules,
32 * but only jQuery core
33 * However, when jQuery is loaded, jQuery-dependent modules already loaded
34 * are run before inline scripts. But if some modules are longer to load and arrive
35 * after jQuery, they will be run after queued inline calls
38 * (c) 2010-2016 Cedric Morin licence GPL
43 * the inline calls queue
48 * the jQuery dependencies xhr loaded queue
53 * the jQuery.getScript queue
58 * the ready function that collect calls and put it in the queue
61 if(typeof f
=='function'){
64 // return jQl in order to support jQuery(document).ready()
67 "getScript":function(s
,c
){
72 * unqueue ready() function
73 * run all queues inline $.ready() calls
74 * in the right order and purge the queue
78 for(var i
=0;i
<jQl
.q
.length
;i
++)
83 * unqueue getScript() function
84 * run all queues $.getScript calls
85 * in the right order and purge the queue
89 for(var i
=0;i
<jQl
.gs
.length
;i
++)
90 jQuery
.getScript(jQl
.gs
[i
][0],jQl
.gs
[i
][1]);
96 * call it after calling jQuery in order to wait it's loaded
97 * or use it in onload='' on script defer/async element
99 * @param function callback
100 * a callback to call after jQuery is loaded
104 "boot":function(callback
){
105 if(typeof window
.jQuery
.fn
== 'undefined'){
107 jQl
.bId
= setInterval(function(){jQl
.boot(callback
)},25);
112 clearInterval(jQl
.bId
);
115 // OK, jQuery is loaded,
116 // we can load additional jQuery dependents modules
119 // then unqueue all getScript calls
122 // and last unqueue all inline calls
123 // (when document is ready)
126 // call the callback if provided
127 if(typeof callback
=='function') callback();
130 "booted":function(){return jQl
.bId
===0},
134 * load jQuery script asynchronously in all browsers
135 * by delayed dom injection
137 * jQuery url to use, can be a CDN hosted,
138 * or a compiled js including jQuery
141 "loadjQ":function(src
,callback
){
144 var s
=document
.createElement('script');
146 document
.getElementsByTagName('head')[0].appendChild(s
)
157 * jQuery-dependant modules loading
158 * this section is not necessary in case of loading only one script
160 * Can be removed to make the booter smaller
170 * load a jQuery-dependent script
171 * parallel loading in most browsers by xhr loading and injection
172 * the jQ-dependant script is queued or run when loaded,
173 * depending of jQuery loading state
175 "loadjQdep":function(src
){
176 jQl
.loadxhr(src
, jQl
.qdep
);
180 * queue jQuery-dependent script if download finish before jQuery loading
181 * or run it directly if jQuery loaded, and previous loaded modules are run
182 * (preserve initial order)
185 * the js script to inject inline in dom
187 * the source url of the script, not used here
189 "qdep":function(txt
, src
){
191 if (typeof window
.jQuery
.fn
!=="undefined" && !jQl
.dq
.length
){
201 * dequeue jQuery-dependent modules loaded before jQuery
204 "unqjQdep":function(){
205 // security, should never happen
206 // so we keep setTimeout even if setInterval would be cleaner
207 if (typeof window
.jQuery
.fn
=="undefined"){
208 setTimeout(jQl
.unqjQdep
, 50);
211 for (var i
=0;i
<jQl
.dq
.length
;i
++) jQl
.rs(jQl
.dq
[i
]);
217 * run a text script as inline js
221 * original source of the script (not used here)
223 "rs":function(txt
, src
){
224 var se
= document
.createElement('script');
225 document
.getElementsByTagName('head')[0].appendChild(se
);
230 * multi-browsers XHr loader,
231 * credits http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/
234 "loadxhr":function(src
,callback
) {
237 xoe
.onreadystatechange = function() {
238 if ( xoe
.readyState
!= 4 || 200 != xoe
.status
) return;
239 callback(xoe
.responseText
,src
);
242 xoe
.open('GET', src
, true);
250 * facilitie for XHr loader
251 * credits http://www.stevesouders.com/blog/2009/04/27/loading-scripts-without-blocking/
257 xhrObj
= new XMLHttpRequest();
260 var progid
= ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
261 for ( var i
=0; i
< progid
.length
; ++i
) {
263 xhrObj
= new ActiveXObject(progid
[i
]);
280 * map $ and jQuery to the jQl.ready() function in order to catch all
281 * inline calls like :
283 * jQuery(function(){...})
284 * $('document').ready(function(){...})
285 * jQuery('document').ready(function(){...})
287 * $.getScript() and jQuery.getScript() also catched and called
289 * only if jQuery is not already loaded
291 if (typeof window
.jQuery
=='undefined'){var $=jQl
.ready
,jQuery
=$;$.getScript
=jQl
.getScript
;}