-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparseScripts.js
More file actions
46 lines (41 loc) · 1.52 KB
/
parseScripts.js
File metadata and controls
46 lines (41 loc) · 1.52 KB
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
44
45
46
/*
* parseScripts
* (c) James Padolsey
* http://james.padolsey.com
*
* Makes it easier to extend JavaScript as you see fit.
* (uses custom type attributes in SCRIPT elements)
* Please read README.text
*/
function parseScripts(scriptType, parseFn) {
var scripts = document.getElementsByTagName('script'),
sLength = scripts.length,
execute = function(parsed) {
// Execute parsed script in global context.
var dScript = document.createElement('script');
try {
dScript.appendChild( document.createTextNode(parsed) );
document.body.appendChild(dScript);
} catch(e) {
dScript.text = parsed;
document.getElementsByTagName('head')[0].appendChild(dScript);
}
dScript.parentNode.removeChild(dScript);
};
while (sLength--) {
// All script elements matching scriptType are passed to parseFn.
var script = scripts[sLength],
type = script.type,
code = script.innerHTML;
if (scriptType.test ? scriptType.test(type) : type === scriptType) {
if (script.src) {
var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
xhr.open('GET', script.src, false);
xhr.send(null);
code = xhr.responseText;
xhr = null;
}
execute(parseFn ? parseFn(code) : code);
}
}
}