开发者

Check if javascript file was loaded

开发者 https://www.devze.com 2023-01-22 20:02 出处:网络
I am loading the following javascript files from a CDN. Is there any way I开发者_开发问答 can check if these have been loaded correctly and if not, load them from a local folder?

I am loading the following javascript files from a CDN. Is there any way I开发者_开发问答 can check if these have been loaded correctly and if not, load them from a local folder?

http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js http://ajax.microsoft.com/ajax/jquery.ui/1.8.5/jquery-ui.min.js http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js

I know I can check the jquery file but not sure about the others. Any tips?


In your HTML, you can do something like this:

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script>!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"><\/script>')</script>

The example shown is loading jquery from Google CDN and falls back on a local copy of JQuery if that fails.

Credits to HTML5 Boilerplate for showing the trick


YepNope.js is a more robust solution for loading resources (like js files), checking whether they've been loaded or not, and then providing a fallback if necessary. This is just 1 feature among many provided by the library.

0

精彩评论

暂无评论...
验证码 换一张
取 消