In my code I am loading a script from a remote server. Here is what I have done:
var loadFlag = false;
var myWidgetScript = document.createElement("script");
myWidgetScript.type = "text/javascript";
myWidgetScript.charset = "utf-8";
myWidgetScript.src = document.location.protocol+ "//mytest.com/loader";
document.getElementsByTagName("head")[0].appendChild(myWidgetScript);
myWidgetScript.onreadystatechange=myWidgetScript.onload = myWidgetScript.onload = function() {
loadFlag = true;
if (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") {
//Do what you want
}
};
if(!loadFlag){
alert("Not loaded");
}
Now I want to generate an alert if script tag has failed to load from the URL. I am incase the URL has a typo or if the server mytest.com is down. But in my code it is generating alert even if the URL is correct and server is up... What is wrong with my code?... Can I have some other way to accomplish this..?