|
1 | | -!function (win, doc, timeout) { |
| 1 | +!function (win, doc) { |
2 | 2 | var head = doc.getElementsByTagName('head')[0], |
3 | 3 | list = {}, ids = {}, delay = {}, scripts = {}, |
4 | | - s = 'string', f = false, push = 'push', |
| 4 | + s = 'string', f = false, push = 'push', complete = /^c|loade/, |
5 | 5 | domContentLoaded = 'DOMContentLoaded', readyState = 'readyState', |
6 | 6 | addEventListener = 'addEventListener', onreadystatechange = 'onreadystatechange', |
7 | 7 | faux = doc.createElement('script'), |
|
17 | 17 | doc[readyState] = 'loading'; |
18 | 18 | } |
19 | 19 |
|
| 20 | + function timeout(fn) { |
| 21 | + setTimeout(fn, 0) |
| 22 | + } |
| 23 | + |
20 | 24 | function every(ar, fn, i) { |
21 | 25 | for (i = 0, j = ar.length; i < j; ++i) if (!fn(ar[i])) return f |
22 | 26 | return 1; |
23 | 27 | } |
24 | 28 |
|
25 | 29 | function each(ar, fn) { |
26 | | - every(ar, function (el) { |
27 | | - return !fn(el); |
28 | | - }) |
| 30 | + every(ar, function (el) { return !fn(el) }) |
29 | 31 | } |
30 | 32 |
|
31 | 33 | function $script(paths, idOrDone, optDone) { |
|
35 | 37 | done = idOrDoneIsDone ? idOrDone : optDone, |
36 | 38 | id = idOrDoneIsDone ? paths.join('') : idOrDone, |
37 | 39 | queue = paths.length |
| 40 | + |
38 | 41 | function loopFn(item) { |
39 | 42 | return item.call ? item() : list[item] |
40 | 43 | } |
| 44 | + |
41 | 45 | function callback() { |
42 | 46 | if (!--queue) { |
43 | 47 | list[id] = 1 |
|
47 | 51 | } |
48 | 52 | } |
49 | 53 | } |
| 54 | + |
50 | 55 | timeout(function () { |
51 | 56 | each(paths, function (path, p) { |
52 | 57 | p = $script.path ? $script.path + path + '.js' : path |
|
56 | 61 | } |
57 | 62 | scripts[path] = 1 |
58 | 63 | ids[id] = id || f |
59 | | - // preload(p, function (el) { |
60 | 64 | !$script.order ? create(p, callback) : preload(p, function (el) { |
61 | 65 | 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 | + } |
64 | 82 | workingPaths[0] == p && create(p, callback) |
65 | 83 | workingPaths.shift() |
66 | 84 | while (loadedPaths[workingPaths[0]]) { |
|
69 | 87 | } |
70 | 88 | }) |
71 | 89 | }) |
72 | | - }, 0) |
| 90 | + }) |
73 | 91 | return $script |
74 | 92 | } |
75 | 93 |
|
76 | 94 | function create(path, fn, type) { |
77 | 95 | var el = doc.createElement('script'), loaded = f |
78 | | - console.log('create("' + path + '")'); |
79 | 96 | el.type = type || 'text/javascript' |
80 | 97 | el.async = !$script.order |
81 | 98 | 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 |
83 | 100 | el.onload = el[onreadystatechange] = null |
84 | 101 | loaded = 1 |
85 | 102 | fn && fn() |
|
88 | 105 | head.insertBefore(el, head.firstChild) |
89 | 106 | } |
90 | 107 |
|
| 108 | + function bind(fn) { |
| 109 | + var a = [].slice.call(aruments, 1) |
| 110 | + return function () { |
| 111 | + fn.apply(null, a) |
| 112 | + } |
| 113 | + } |
| 114 | + |
91 | 115 | function preload(path, fn, el) { |
92 | | - el = doc.createElement('script') |
93 | 116 | if (preloadReal) { |
| 117 | + el = doc.createElement('script') |
| 118 | + el.type = 'text/javascript' |
94 | 119 | if (preloadExplicit) { |
95 | 120 | el.preload = true |
96 | | - el.onpreload = function () { |
97 | | - fn(el) |
98 | | - } |
| 121 | + el.onpreload = bind(fn,el) |
99 | 122 | } 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 | + } |
103 | 128 | } |
104 | 129 | } |
105 | | - script.src = path |
| 130 | + el.src = path |
106 | 131 | } else if (async) { |
107 | 132 | create(path, fn) |
108 | 133 | } else { |
|
134 | 159 | return this; |
135 | 160 | }; |
136 | 161 |
|
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 |
140 | 165 |
|
141 | | -}(this, document, setTimeout); |
| 166 | +}(this, document); |
0 commit comments