-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.ready.js
44 lines (37 loc) · 1.3 KB
/
dom.ready.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
/*
A Function for Watching the DOM Until It's Ready/Loaded
*/
function domReady( f ) {
// If the DOM is already loaded, execute the function right away
if( domReady.done ) return f();
// If we've already added a function
if( domReady.timer ) {
// Add it to the list of functions to execute
domReady.ready.push( f );
} else {
// Attach an event for when the page finishes loading,
// just in case it finishes first. Usee addEvent.
window.addEventListener( 'load', isDOMReady );
// Initialize the array of functions to execute
domReady.ready = [ f ];
// Check to see if the DOM is ready as quickly as possible
domReady.timer = setInterval( isDOMReady, 13 );
}
}
// Checks to see if the DOM is ready for navigation
function isDOMReady() {
// If we already figured out that the page is ready, ignore
if( domReady.done ) return false;
// Check to see if a number of functions and elements are able to be accessed
if( document && document.getElementsByTagName && document.getElementById && document.body ) {
// If they are ready, we can stop checking
domReady.timer = null;
// Execute all the functions that were waiting
for ( var i =0; i < domReady.ready.length; i++ ) {
domReady.ready[ i ]();
}
// Remeber that we are now done
domReady.ready = null;
domReady.done = true;
}
}