src - Remote Script fail handling - JavaScript -
in code loading script remote server. here 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 want } }; if(!loadflag){ alert("not loaded"); }
now want generate alert if script tag has failed load url. incase url has typo or if server mytest.com down. in code generating alert if url correct , server up... wrong code?... can have other way accomplish this..?
try this... may help.
var headtag = document.getelementsbytagname("head")[0]; var jqtag = document.createelement('script'); jqtag.type = 'text/javascript'; jqtag.src = 'your script'; headtag.appendchild(jqtag); if (jqtag) { if (jqtag.readystate) { // old versions of ie jqtag.onreadystatechange = function() { if (this.readystate == 'complete' || this.readystate == 'loaded') { alert('loaded'); } }; } else { // other browsers jqtag.onload = function() { alert('loaded'); }; } }
Comments
Post a Comment