This question already has an answer here:
I’m using a CDN for the following javascript:
- https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
- https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js
- http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js
- http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.min.js
For each one, how can I revert to using local copy in the instance where it may be blocked/unavailable?
You need to know, how you can make sure that a lib was loaded successfully. For instance:
So this trys to load jQuery (1.5.1) from the google CDN. Since
<script>
tags do block the overall render & execution process (if not explicitly told different), we can check right after that if thejQuery
object is found withinwindow
. If not, just fallback by writing another<script>
tag into the document, referencing a local copy.I use http://fallback.io/
Best to do all this script loading with your own Javascript code.
First try to load the CDN file by inserting a new SCRIPT element into the DOM. Then check that it has loaded by looking for an object that it defines. If the object does not appear, then insert another SCRIPT element to load the local copy. Probably best to clean up the DOM and remove SCRIPTs which failed to load as well.
Don't forget to account for timing issues, i.e. load is not instant.
I answered a similar questions at jquery ui - how to use google CDN
You can make the call using
You can also link to other Ui themes by changes the name of the theme. In This case change the name base to any other theme name
/base/jquery-ui.css
to any other theme.Check out the jQuery UI Blog for a link of all CDN links http://blog.jqueryui.com/
If you want to revert back to your host in case Google failed, you can do
I don't think any of the solutions could be painless if the related CDN is really filtered out. (like for example iptables/drop, misconfigured routers.) If you don't trust them use everything locally... And you spare yourself a few surprises/calls from users on exotic network.