-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathondomready.js
152 lines (126 loc) · 4.7 KB
/
ondomready.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*!
* onDomReady.js 1.4.0 (c) 2013 Tubal Martin - MIT license
*/
;(function (definition) {
if (typeof define === "function" && define.amd) {
// Register as an AMD module.
define(definition);
} else {
// Browser globals
window['onDomReady'] = definition();
}
}(function() {
'use strict';
var win = window,
doc = win.document,
docElem = doc.documentElement,
LOAD = "load",
FALSE = false,
ONLOAD = "on"+LOAD,
COMPLETE = "complete",
READYSTATE = "readyState",
ATTACHEVENT = "attachEvent",
DETACHEVENT = "detachEvent",
ADDEVENTLISTENER = "addEventListener",
DOMCONTENTLOADED = "DOMContentLoaded",
ONREADYSTATECHANGE = "onreadystatechange",
REMOVEEVENTLISTENER = "removeEventListener",
// W3C Event model
w3c = ADDEVENTLISTENER in doc,
top = FALSE,
// isReady: Is the DOM ready to be used? Set to true once it occurs.
isReady = FALSE,
// Callbacks pending execution until DOM is ready
callbacks = [];
// Handle when the DOM is ready
function ready( fn ) {
if ( !isReady ) {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if ( !doc.body ) {
return defer( ready );
}
// Remember that the DOM is ready
isReady = true;
// Execute all callbacks
while ( fn = callbacks.shift() ) {
defer( fn );
}
}
}
// The ready event handler
function completed( event ) {
// readyState === "complete" is good enough for us to call the dom ready in oldIE
if ( w3c || event.type === LOAD || doc[READYSTATE] === COMPLETE ) {
detach();
ready();
}
}
// Clean-up method for dom ready events
function detach() {
if ( w3c ) {
doc[REMOVEEVENTLISTENER]( DOMCONTENTLOADED, completed, FALSE );
win[REMOVEEVENTLISTENER]( LOAD, completed, FALSE );
} else {
doc[DETACHEVENT]( ONREADYSTATECHANGE, completed );
win[DETACHEVENT]( ONLOAD, completed );
}
}
// Defers a function, scheduling it to run after the current call stack has cleared.
function defer( fn, wait ) {
// Allow 0 to be passed
setTimeout( fn, +wait >= 0 ? wait : 1 );
}
// Attach the listeners:
// Catch cases where onDomReady is called after the browser event has already occurred.
// we once tried to use readyState "interactive" here, but it caused issues like the one
// discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
if ( doc[READYSTATE] === COMPLETE ) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
defer( ready );
// Standards-based browsers support DOMContentLoaded
} else if ( w3c ) {
// Use the handy event callback
doc[ADDEVENTLISTENER]( DOMCONTENTLOADED, completed, FALSE );
// A fallback to window.onload, that will always work
win[ADDEVENTLISTENER]( LOAD, completed, FALSE );
// If IE event model is used
} else {
// Ensure firing before onload, maybe late but safe also for iframes
doc[ATTACHEVENT]( ONREADYSTATECHANGE, completed );
// A fallback to window.onload, that will always work
win[ATTACHEVENT]( ONLOAD, completed );
// If IE and not a frame
// continually check to see if the document is ready
try {
top = win.frameElement == null && docElem;
} catch(e) {}
if ( top && top.doScroll ) {
(function doScrollCheck() {
if ( !isReady ) {
try {
// Use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
top.doScroll("left");
} catch(e) {
return defer( doScrollCheck, 50 );
}
// detach all dom ready events
detach();
// and execute any waiting functions
ready();
}
})();
}
}
function onDomReady( fn ) {
// If DOM is ready, execute the function (async), otherwise wait
isReady ? defer( fn ) : callbacks.push( fn );
}
// Add version
onDomReady.version = "1.4.0";
// Add method to check if DOM is ready
onDomReady.isReady = function(){
return isReady;
};
return onDomReady;
}));