tags:

views:

44

answers:

4

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

A: 

One way would be to define a variable in the script you include and then check whether this variable is defined, or not.

JochenJung
The problem is... When do you check for the variable? The `<script>` loads asynchronously, and attaching events to `<script>` elements is not reliable: http://unixpapa.com/js/dyna.html
Daniel Vassallo
A: 

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);
Andrew Dunn
It's not pretty easy. They will trigger that update if the script was loaded. I am asking how to detect if the script was not there.
Lx1
+1  A: 

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();
Daniel Vassallo
I like your solution.
Lx1
I wish though there was a better one. I could think decide to timeout just a millisecond before the answer comes back if I deal with a slow server.
Lx1
@Lx1: Yes, you could reduce the timeout of setTimeout to 10 or 20 milliseconds actually. If I remember correctly 10 is the minimum timeout for most browsers.
Daniel Vassallo
A: 

What JavaScript library do you use?

In jQuery if you load the script via Ajax, you have an option to run a success callback function (and other types of callbacks)...

There is also a dedicated function for loading scripts: $.getScript()

Šime Vidas
There is no ajax.
Lx1
What do you mean?
Šime Vidas