Implement 'error' callback for Dynamically loaded javascript file -
i loading javascript dynamically , accessing variable. checked second answer question , following code works beautifully.
include = function (url, fn) { var e = document.createelement("script"); e.onload = fn; e.src = url; e.async=true; document.getelementsbytagname("head")[0].appendchild(e); }; include("test.js",function(){ console.log(foo); }); question: want have onfailure callback function allows me process code in case
- the internet down.
- the filename not accessible (eg: incorrect path)
will appreciate if can guide me right direction. thanks
the mdn page htmlscriptelement has a example can build off.
function loaderror (oerror) { throw new urierror("the script " + oerror.target.src + " not accessible."); } function importscript (ssrc, fonload) { var oscript = document.createelement("script"); oscript.type = "text\/javascript"; oscript.onerror = loaderror; if (fonload) { oscript.onload = fonload; } document.currentscript.parentnode.insertbefore(oscript, document.currentscript); oscript.src = ssrc; }
Comments
Post a Comment