/
importscripts.js
43 lines (38 loc) · 1.38 KB
/
importscripts.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
import { register, injectElement } from "../util/index";
import { DOM } from "../const";
import { StaticMethodError } from "../errors";
register({
/**
* Import external scripts on the page and call optional callback when it will be done
* @memberof $Document#
* @alias $Document#importScripts
* @param {...String} urls script file urls
* @param {Function} [callback] callback that is triggered when all scripts are loaded
* @example
* DOM.importScripts("http://cdn/script1.js", function() {
* // do something when the script is loaded
* });
* // loading several scripts sequentially
* DOM.importScripts("http://cdn/script2.js", "http://cdn/script3.js");
*/
importScripts(...urls) {
var doc = this[0].ownerDocument;
var callback = () => {
var arg = urls.shift(),
argType = typeof arg,
script;
if (argType === "string") {
script = doc.createElement("script");
script.src = arg;
script.onload = callback;
script.async = true;
injectElement(script);
} else if (argType === "function") {
arg();
} else if (arg) {
throw new StaticMethodError("importScripts", arguments);
}
};
callback();
}
});