-
Notifications
You must be signed in to change notification settings - Fork 0
/
domReady.js
78 lines (64 loc) · 1.74 KB
/
domReady.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
//window.onload = sdk.createFrame;
/**
* [domReady]
* @param {Function} fun 执行的函数
* @return {null}
*/
var isReady = false,
scrollIntervalId,
queue = [];
function domReady(callback) {
var testDiv,
isTop;
// justage
if (domReady.done)
return callback();
if (!isReady || queue != null) {
// 把参数中的函数放入执行队列
queue.push(callback);
}
if (window.addEventListener) {
window.addEventListener('load', isDOMReady, false);
} else {
window.attachEvent('onload', isDOMReady);
testDiv = document.createElement('div');
try {
isTop = window.frameElement === null;
} catch (e) {}
// 判断IE http://javascript.nwbox.com/IEContentLoaded/
if (testDiv.doScroll && isTop && window.external) {
scrollIntervalId = setInterval(function() {
try {
testDiv.doScroll();
isDOMReady();
} catch (e) {}
}, 30);
}
}
if (document.readyState === "complete") {
isDOMReady();
}
}
function isDOMReady() {
if (!isReady) {
isReady = true;
if (scrollIntervalId) {
clearInterval(scrollIntervalId);
}
runFunction();
}
}
function runFunction() {
if (domReady.done)
return false;
// 执行所有正在队列中的函数
for (var i = 0; i < queue.length; i++) {
queue[i]();
//queue.shift();
// 防止不执行内嵌domReady函数
if (queue.length == 0) {
queue = null;
domReady.done = true;
}
}
}