]>
Commit | Line | Data |
---|---|---|
f0cfe83e AD |
1 | define("dojo/domReady", ['./has'], function(has){ |
2 | var global = this, | |
3 | doc = document, | |
4 | readyStates = { 'loaded': 1, 'complete': 1 }, | |
5 | fixReadyState = typeof doc.readyState != "string", | |
6 | ready = !!readyStates[doc.readyState]; | |
7 | ||
8 | // For FF <= 3.5 | |
9 | if(fixReadyState){ doc.readyState = "loading"; } | |
10 | ||
11 | if(!ready){ | |
12 | var readyQ = [], tests = [], | |
13 | detectReady = function(evt){ | |
14 | evt = evt || global.event; | |
15 | if(ready || (evt.type == "readystatechange" && !readyStates[doc.readyState])){ return; } | |
16 | ready = 1; | |
17 | ||
18 | // For FF <= 3.5 | |
19 | if(fixReadyState){ doc.readyState = "complete"; } | |
20 | ||
21 | while(readyQ.length){ | |
22 | (readyQ.shift())(doc); | |
23 | } | |
24 | }, | |
25 | on = function(node, event){ | |
26 | node.addEventListener(event, detectReady, false); | |
27 | readyQ.push(function(){ node.removeEventListener(event, detectReady, false); }); | |
28 | }; | |
29 | ||
30 | if(!has("dom-addeventlistener")){ | |
31 | on = function(node, event){ | |
32 | event = "on" + event; | |
33 | node.attachEvent(event, detectReady); | |
34 | readyQ.push(function(){ node.detachEvent(event, detectReady); }); | |
35 | }; | |
36 | ||
37 | var div = doc.createElement("div"); | |
38 | try{ | |
39 | if(div.doScroll && global.frameElement === null){ | |
40 | // the doScroll test is only useful if we're in the top-most frame | |
41 | tests.push(function(){ | |
42 | // Derived with permission from Diego Perini's IEContentLoaded | |
43 | // http://javascript.nwbox.com/IEContentLoaded/ | |
44 | try{ | |
45 | div.doScroll("left"); | |
46 | return 1; | |
47 | }catch(e){} | |
48 | }); | |
49 | } | |
50 | }catch(e){} | |
51 | } | |
52 | ||
53 | on(doc, "DOMContentLoaded"); | |
54 | on(global, "load"); | |
55 | ||
56 | if("onreadystatechange" in doc){ | |
57 | on(doc, "readystatechange"); | |
58 | }else if(!fixReadyState){ | |
59 | // if the ready state property exists and there's | |
60 | // no readystatechange event, poll for the state | |
61 | // to change | |
62 | tests.push(function(){ | |
63 | return readyStates[doc.readyState]; | |
64 | }); | |
65 | } | |
66 | ||
67 | if(tests.length){ | |
68 | var poller = function(){ | |
69 | if(ready){ return; } | |
70 | var i = tests.length; | |
71 | while(i--){ | |
72 | if(tests[i]()){ | |
73 | detectReady("poller"); | |
74 | return; | |
75 | } | |
76 | } | |
77 | setTimeout(poller, 30); | |
78 | }; | |
79 | poller(); | |
80 | } | |
81 | } | |
82 | ||
83 | function domReady(callback){ | |
84 | // summary: | |
85 | // Plugin to delay require()/define() callback from firing until the DOM has finished loading. | |
86 | if(ready){ | |
87 | callback(doc); | |
88 | }else{ | |
89 | readyQ.push(callback); | |
90 | } | |
91 | } | |
92 | domReady.load = function(id, req, load){ | |
93 | domReady(load); | |
94 | }; | |
95 | ||
96 | return domReady; | |
97 | }); |