-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_script.js
24 lines (23 loc) · 972 Bytes
/
add_script.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
var add_script = function add_script(src="", if_not_already_exists=true) {
if (!!if_not_already_exists) {
// if_not_already_exsists should be a css selector
if (if_not_already_exists===true) {
if_not_already_exists = `script[src='${src}']`
}
if(document.querySelectorAll(if_not_already_exists).length==1) {
// might as well return matching elements so it looks coherent
return document.querySelector(if_not_already_exists);
} else if(document.querySelectorAll(if_not_already_exists).length>1) {
// might as well return matching elements so it looks coherent, this lists all
return document.querySelectorAll(if_not_already_exists);
}
}
let script=document.createElement("script");
script.src=src;
document.head.append(script);
// return object in case user wants to modify
return script;
}
var add_script_iter = function add_script_iter(...scripts) {
scripts.forEach(script => add_script(...script))
}