I am using the following technique to load up Javascript dynamically:
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "file.js";
document.body.appendChild(script);
It’s quite a common approach.
It’s also discussed here: http://www.nczonline.net/blog/2009/06/23/loading-javascript-without-blocking/
I know how to get notified once the file has been loaded and executed
What I don’t know is that if the link to the Javascript source file is broken how can I be notified.
Thanks
Listening for events on script elements is not considered reliable (Source). One option that comes to mind is to use
setTimeout()to poll for a variable that you expect to be defined in your external script. Afterxseconds, you could timeout your poll and consider the script as broken.External Script: file.js:
Main document: