comparison default/node_modules/shoestring/src/core/ready.js @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 //>>excludeStart("exclude", pragmas.exclude);
2 define([ "shoestring" ], function(){
3 //>>excludeEnd("exclude");
4
5 /**
6 * Bind callbacks to be run when the DOM is "ready".
7 *
8 * @param {function} fn The callback to be run
9 * @return shoestring
10 * @this shoestring
11 */
12 shoestring.ready = function( fn ){
13 if( ready && fn ){
14 fn.call( doc );
15 }
16 else if( fn ){
17 readyQueue.push( fn );
18 }
19 else {
20 runReady();
21 }
22
23 return [doc];
24 };
25
26 // TODO necessary?
27 shoestring.fn.ready = function( fn ){
28 shoestring.ready( fn );
29 return this;
30 };
31
32 // Empty and exec the ready queue
33 var ready = false,
34 readyQueue = [],
35 runReady = function(){
36 if( !ready ){
37 while( readyQueue.length ){
38 readyQueue.shift().call( doc );
39 }
40 ready = true;
41 }
42 };
43
44 // If DOM is already ready at exec time, depends on the browser.
45 // From: https://github.com/mobify/mobifyjs/blob/526841be5509e28fc949038021799e4223479f8d/src/capture.js#L128
46 if (doc.attachEvent ? doc.readyState === "complete" : doc.readyState !== "loading") {
47 runReady();
48 } else {
49 doc.addEventListener( "DOMContentLoaded", runReady, false );
50 doc.addEventListener( "readystatechange", runReady, false );
51 win.addEventListener( "load", runReady, false );
52 }
53
54 //>>excludeStart("exclude", pragmas.exclude);
55 });
56 //>>excludeEnd("exclude");