I need to include an external javascript file, but if it takes over 2 seconds, I want to remove it and block it from running. The following code successfully removes and block the external script from running in IE and Chrome, but not Firefox.
Note: jQuery dependency
Any idea why the external script still runs in Firefox, even after I reset the variable and remove it from the DOM? Thanks for the help!
Note: jQuery dependency
Code:
var gTimeout, gScript;
$(function() {
alert("Static page fully loaded. Adding server script.");
loadScript("[URL unfurl="true"]http://server/script.js",[/URL] afterLoad);
gTimeout = setTimeout("destroyScript()", 2000);
});
function loadScript(url, callback){
gScript = document.createElement("script");
gScript.type = "text/javascript";
gScript.id = "externalScript";
if (gScript.readyState) { //IE
gScript.onreadystatechange = function() {
if (gScript.readyState == "loaded" || gScript.readyState == "complete") {
clearTimeout(gTimeout);
gScript.onreadystatechange = null;
callback();
}
};
} else { //Others
gScript.onload = function() {
clearTimeout(gTimeout);
callback();
};
}
gScript.src = url;
document.documentElement.insertBefore(gScript, document.documentElement.firstChild);
}
function afterLoad() {
clearTimeout(gTimeout);
}
function destroyScript() {
gScript = null;
$("#externalScript").remove();
alert("Script took too long and was removed");
}
Any idea why the external script still runs in Firefox, even after I reset the variable and remove it from the DOM? Thanks for the help!