Reputation: 111
This is my code so far, and the main problem is that the scripts are sometimes loaded after the main page is loaded, a page is requested with AJAX and uses parseScript on the output, is there a way to make this function pause and not return anything until all the scripts are loaded by the browser?
function addScriptTag(src) {
var node = document.getElementsByTagName("head")[0] || document.body;
if(node){
var script = document.createElement("script");
script.type="text/javascript";
script.src=src
node.appendChild(script);
} else {
document.write("<script src='"+src+"' type='text/javascript'></script>");
}
}
function parseScript(_source) {
var source = _source;
var scripts = source.match(/<script[^>]*src=[^>]*>/g);
if (scripts) {
for (var i = 0; i < scripts.length; i++) {
src = scripts[i].match(/src=("([^"]*)"|'([^']*)')/);
src = src[2] || src[3];
if (src) {
addScriptTag(src);
}
}
}
var scripts = new Array();
return source;
}
Upvotes: 0
Views: 80
Reputation: 9520
Use window.onload or jquery $(document).ready and then call this function inside ? Or call the function at the bottom of the page.
Upvotes: 3