简体   繁体   中英

How to detect is file accessible on different domain with JavaScript?

I am using 3th party CDN servers for my javascript frameworks. But sometime these servers crash and whole my web site can not be rendered. So I thinking to make first look up does file exist on that server if not use local server. Does anyone knows some good solution for it?

<script> !window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.2.js"%3E%3C/script%3E'))</script>

Source: http://weblogs.asp.net/jgalloway/archive/2010/01/21/using-cdn-hosted-jquery-with-a-local-fall-back-copy.aspx

You can try to use a function defined in one of the js files loaded by the cdn and if it's not set you can load a local copy of it.

You can see this implemented in many webpages (HTML5 Boilerplate http://html5boilerplate.com/ )

You could use a scriptloader, like YepNope :

yepnope.js has the capability to do resource fallbacks and still download dependent scripts in parallel with the first. More clearly:

yepnope([{
  load: 'http:/­/ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
  complete: function () {
    if (!window.jQuery) {
      yepnope('local/jquery.min.js');
    }
  }
}, {
  load: 'jquery.plugin.js',
  complete: function () {
    jQuery(function () {
      jQuery('div').plugin();
    });
  }
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM