tags:

views:

21

answers:

1

Hey is there a way to specify a fallback js just in case loading from the YUI CDN were to fail?

+1  A: 

The simplest solution is to check for the existence of the global object/function created by the script. For instance, for jQuery it would be

typeof jQuery === 'undefined';

and YUI I believe is

typeof YUI === 'undefined';

Then you might want to try injecting the script in some other manner, like

if(typeof YUI === 'undefined'){
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "some/other/source.js";
    head.appendChild(script);
}

This will create a new script element in your head with a link to another source of your choice.

Yi Jiang