Skip to content

Commit 093b0a1

Browse files
committed
- more changes for ie
1 parent 366d7b9 commit 093b0a1

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

src/script.js

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
!function (win, doc, timeout) {
1+
!function (win, doc) {
22
var head = doc.getElementsByTagName('head')[0],
33
list = {}, ids = {}, delay = {}, scripts = {},
4-
s = 'string', f = false, push = 'push',
4+
s = 'string', f = false, push = 'push', complete = /^c|loade/,
55
domContentLoaded = 'DOMContentLoaded', readyState = 'readyState',
66
addEventListener = 'addEventListener', onreadystatechange = 'onreadystatechange',
77
faux = doc.createElement('script'),
@@ -17,15 +17,17 @@
1717
doc[readyState] = 'loading';
1818
}
1919

20+
function timeout(fn) {
21+
setTimeout(fn, 0)
22+
}
23+
2024
function every(ar, fn, i) {
2125
for (i = 0, j = ar.length; i < j; ++i) if (!fn(ar[i])) return f
2226
return 1;
2327
}
2428

2529
function each(ar, fn) {
26-
every(ar, function (el) {
27-
return !fn(el);
28-
})
30+
every(ar, function (el) { return !fn(el) })
2931
}
3032

3133
function $script(paths, idOrDone, optDone) {
@@ -35,9 +37,11 @@
3537
done = idOrDoneIsDone ? idOrDone : optDone,
3638
id = idOrDoneIsDone ? paths.join('') : idOrDone,
3739
queue = paths.length
40+
3841
function loopFn(item) {
3942
return item.call ? item() : list[item]
4043
}
44+
4145
function callback() {
4246
if (!--queue) {
4347
list[id] = 1
@@ -47,6 +51,7 @@
4751
}
4852
}
4953
}
54+
5055
timeout(function () {
5156
each(paths, function (path, p) {
5257
p = $script.path ? $script.path + path + '.js' : path
@@ -56,11 +61,24 @@
5661
}
5762
scripts[path] = 1
5863
ids[id] = id || f
59-
// preload(p, function (el) {
6064
!$script.order ? create(p, callback) : preload(p, function (el) {
6165
if (async) return callback()
62-
el && head.insertBefore(el, head.firstChild)
63-
loadedPaths[p] = 1
66+
loadedPaths[p] = el || 1
67+
if (el) {
68+
if (workingPaths[0] == p) {
69+
head.insertBefore(el, head.firstChild)
70+
timeout(function () {
71+
callback()
72+
workingPaths.shift()
73+
while (loadedPaths[workingPaths[0]]) {
74+
head.insertBefore(loadedPaths[workingPaths[0]], head.firstChild)
75+
timeout(callback)
76+
workingPaths.shift()
77+
}
78+
})
79+
}
80+
return
81+
}
6482
workingPaths[0] == p && create(p, callback)
6583
workingPaths.shift()
6684
while (loadedPaths[workingPaths[0]]) {
@@ -69,17 +87,16 @@
6987
}
7088
})
7189
})
72-
}, 0)
90+
})
7391
return $script
7492
}
7593

7694
function create(path, fn, type) {
7795
var el = doc.createElement('script'), loaded = f
78-
console.log('create("' + path + '")');
7996
el.type = type || 'text/javascript'
8097
el.async = !$script.order
8198
el.onload = el[onreadystatechange] = function () {
82-
if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return
99+
if ((el[readyState] && !(complete.test(el[readyState]))) || loaded) return
83100
el.onload = el[onreadystatechange] = null
84101
loaded = 1
85102
fn && fn()
@@ -88,21 +105,29 @@
88105
head.insertBefore(el, head.firstChild)
89106
}
90107

108+
function bind(fn) {
109+
var a = [].slice.call(aruments, 1)
110+
return function () {
111+
fn.apply(null, a)
112+
}
113+
}
114+
91115
function preload(path, fn, el) {
92-
el = doc.createElement('script')
93116
if (preloadReal) {
117+
el = doc.createElement('script')
118+
el.type = 'text/javascript'
94119
if (preloadExplicit) {
95120
el.preload = true
96-
el.onpreload = function () {
97-
fn(el)
98-
}
121+
el.onpreload = bind(fn,el)
99122
} else {
100-
el[onreadystatechange] = function(){
101-
el[readyState] == 'loaded' && fn(el)
102-
el[onreadystatechange] = null
123+
el[onreadystatechange] = function () {
124+
if (complete.test(el[readyState])) {
125+
fn(el)
126+
el[onreadystatechange] = null
127+
}
103128
}
104129
}
105-
script.src = path
130+
el.src = path
106131
} else if (async) {
107132
create(path, fn)
108133
} else {
@@ -134,8 +159,8 @@
134159
return this;
135160
};
136161

137-
(typeof module !== 'undefined' && module.exports) ?
138-
(module.exports = $script) :
139-
(win['$script'] = $script);
162+
typeof module !== 'undefined' && module.exports && (module.exports = $script)
163+
164+
win['$script'] = $script
140165

141-
}(this, document, setTimeout);
166+
}(this, document);

0 commit comments

Comments
 (0)