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
Pass the URL of JavaScript file in a <script> tag. Set the onload parameter, Trigger alert if script loaded. If not then check for loaded variable, if it is equal to false, then script not loaded.
Dynamic loading In some situations, we want to load third-party script files dynamically in JavaScript. Those files can be loaded asynchronously in JavaScript. To load a JavaScript file dynamically: Create a script element. Set the src , async , and type attributes.
Both AJAX and dynamic JavaScript refer to ways of using JavaScript. Both are methods of creating an action on a web page without loading that entire page in the browser. AJAX and dynamic JavaScript can make a website faster and more responsive to the user.
We start by creating an empty <script></script> tag in the memory as script and then assign the necessary attributes to its src and the id to identify the script later. Finally, we append the script to our <body></body> tag to actually load this.
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. After x
seconds, you could timeout your poll and consider the script as broken.
External Script: file.js:
var MyLibrary = { };
Main document:
var poll;
var timeout = 100; // 10 seconds timeout
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'file.js';
document.body.appendChild(script);
poll = function () {
setTimeout(function () {
timeout--;
if (typeof MyLibrary !== 'undefined') {
// External file loaded
}
else if (timeout > 0) {
poll();
}
else {
// External library failed to load
}
}, 100);
};
poll();
It's pretty easy, Internet Explorer will trigger an onreadystatechange
event while others will trigger a onload
event for the script object.
var newScript;
var loadFunc = function ()
{
alert("External Javascript File has been loaded");
};
newScript = document.createElement('script');
newScript.setAttribute('type','text/javascript');
newScript.setAttribute('src','file.js');
//IE triggers this event when the file is loaded
if (elm.attachEvent)
{
newScript.attachEvent('onreadystatechange',function()
{
if (newScript.readyState == 'complete' || newScript.readyState == 'loaded')
loadFunc();
});
}
//Other browsers trigger this one
if (newScript.addEventListener)
newScript.addEventListener('load', loadFunc, false);
document.getElementsByTagName('head')[0].appendChild(newScript);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With