source: Dev/branches/rest-dojo-ui/client/dojo/domReady.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 2.2 KB
Line 
1define(['./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())();
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                if(ready){
85                        callback(1);
86                }else{
87                        readyQ.push(callback);
88                }
89        }
90        domReady.load = function(id, req, load){
91                domReady(load);
92        };
93
94        return domReady;
95});
Note: See TracBrowser for help on using the repository browser.