-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadscript.js
28 lines (25 loc) · 904 Bytes
/
loadscript.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
// Take from https://github.com/tserkov/vue-plugin-load-script/blob/master/index.js
export default function (src) {
return new Promise(function (resolve, reject) {
let shouldAppend = false;
let el = document.querySelector('script[src="' + src + '"]');
if (!el) {
el = document.createElement('script');
el.type = 'text/javascript';
el.async = true;
el.src = src;
shouldAppend = true;
}
else if (el.hasAttribute('data-loaded')) {
resolve(el);
return;
}
el.addEventListener('error', reject);
el.addEventListener('abort', reject);
el.addEventListener('load', function loadScriptHandler() {
el.setAttribute('data-loaded', true);
resolve(el);
});
if (shouldAppend) document.head.appendChild(el);
});
};