-
Notifications
You must be signed in to change notification settings - Fork 1
/
bindReady.js
83 lines (79 loc) · 2.74 KB
/
bindReady.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
function bindReady(callback){
var isReady = false;
function ready(){
if(isReady)return;
isReady = true;
callback();
}
bindReady = function(){};
var userAgent = navigator.userAgent.toLowerCase();
var browser = {
safari: /webkit/.test( userAgent ),
opera: /opera/.test( userAgent ),
msie: /msie/.test( userAgent ) && !/opera/.test( userAgent )
};
// Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
if ( document.addEventListener && !browser.opera)
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", ready, false );
// If IE is used and is not in a frame
// Continually check to see if the document is ready
if ( browser.msie && window == top ) (function(){
if (isReady) return;
try {
// If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
document.documentElement.doScroll("left");
} catch( error ) {
setTimeout( arguments.callee, 0 );
return;
}
// and execute a waiting function
ready();
})();
if ( browser.opera )
document.addEventListener( "DOMContentLoaded", function () {
if (isReady) return;
for (var i = 0; i < document.styleSheets.length; i++)
if (document.styleSheets[i].disabled) {
setTimeout( arguments.callee, 0 );
return;
}
// and execute a waiting function
ready();
}, false);
if ( browser.safari ) {
var numStyles;
function countNumStyles(){
var d = document;
var stylesLength = d.getElementsByTagName('style').length;
var links = d.getElementsByTagName('link');
for(var i = 0; i++; i < links.length){
if(links[i].rel == 'stylesheet'){
stylesLength++;
}
}
return stylesLength;
}
(function(){
if (isReady) return;
if ( document.readyState != "loaded" && document.readyState != "complete" ) {
setTimeout( arguments.callee, 0 );
return;
}
if ( numStyles === undefined )
numStyles = countNumStyles();
if ( document.styleSheets.length != numStyles ) {
setTimeout( arguments.callee, 0 );
return;
}
// and execute a waiting function
ready();
})();
}
var oldOnload = window.onload;
window.onload=function(){
if(oldOnload)oldOnload();
ready();
};
}