views:

56

answers:

2

I'm trying to integrate a script file hosted by a third party into a new web site.

Currently, I'm adding a SCRIPT tag to the DOM for that third-party script file on document ready:

$(document).ready( function() {

    var extScript = document.createElement('script');
        extScript.type = 'text/javascript';
        extScript.src = 'http://third-party.com/scriptfile.js';

    $('head').append(extScript);

});

function extScriptCallback() {
    $('#extWidgetContainer').show();
}

But sometimes that third-party script file request times out or takes a long time to respond.

So, for the sake of best practice, I want to provide alternative content if the external script takes longer than e.g. 10 seconds to load.

How do I achieve this? I've looked at JavaScript's native setTimeout(), as well as jQuery's delay() function, but I'm not sure which I should use--or how.

Grateful for any suggestions.

A: 

look at my answer at http://stackoverflow.com/questions/2743850/how-check-if-ajax-googleapis-com-blocked-and-use-my-hosting-of-jquery

Unfortuantely, there is not event like 'script has finshed loading', so you have to go with a delay (setTimeout or jQuery delay are both fine)

Kind Regards

--Andy

jAndy
+1  A: 

There are a couple of ways you can approach this. The first method would be to check on a timer for the existence of a function or variable provided by the third party script. If it's not available when you check for it, it's not loaded yet:

$(document).ready( function() {  
    var extScript = document.createElement('script');
        extScript.type = 'text/javascript';
        extScript.src = 'http://third-party.com/scriptfile.js';

    window.setTimeout(function ()
    {
        if ("thirdPartyFuncName" in window)
            alert("loaded");
        else
            alert("Not loaded yet");
    }, 10000); // 10 secs

    $('head').append(extScript);
}); 

The other way you could go is to investigate the onload and onreadystatechange events provided by browsers. AFAIK, onload is widely implemented, so you could do something like this:

var timer;
extScript.onload = function () { clearTimeout(timer); }
timer = window.setTimeout(function ()
{
    alert("not loaded");
}, 10000); // 10 secs

The downside to this approach is that the load event will fire even if the script has a syntax error, so I still think the first approach is your best bet, because it checks for functionality existence.

Andy E
why not use both methods? Also, you can use onerror to detect scripts if they fail to load.
David Murdoch
@David Murdoch: I'm not 100% sure on the compatibility or reliability of the `onload` and `onerror` methods, or what conditions need to be met for them to fire -- e.g. does `onerror` only apply to network-related errors or do parsing errors fire it too? You could use both methods but I think the first alone should suffice.
Andy E
@David Murdoch: it also appears that IE doesn't support `onerror` for script elements, although the documentation says otherwise: http://stackoverflow.com/questions/2027849/how-to-trigger-script-onerror-in-internet-explorer/2032014#2032014
Andy E
well, ie sucks. It can still be used for the other browsers, right?
David Murdoch
@David: Sure, I think most, if not all, other popular browsers support `onerror`. I can't say it's something I've played around with much, tbh.
Andy E
Thanks, Andy. Your first method looks sound, so I'll be going with that one. Huge help, much appreciated.
markedup