3 ms·
Seems like a graceful failure -- jQuery is still responding to calls. For anyone that this freaks out and wonders how they can utilize the googleapis service i
by rkalla 15y ago
Seems like a graceful failure -- jQuery is still responding to calls.
For anyone that this freaks out and wonders how they can utilize the googleapis service in the future but have a fallback incase things fall apart, here is a fun little snippet that I got from the HTML5 boilerplate[1] project to help:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="/public/scripts/libs/jquery-1.4.4.min.js"%3E%3C/script%3E'))</script>
More or less, you try the Google CDN, then you try and execute your own script right after that checks for the existence of an API-provided element, if it's not there then the CDN failed and you load it directly from your own server.
TIP: You can use "!jQuery.ui" as the check for jQuery-UI if you are using that.
The first time I saw this approach I thought "well that's ugly", but it's the 0.01% of the time that it gets you out of a bind that I think makes it worth it.
Q: Does anyone happen to know if scripts are loaded serially in all browsers? Is there ever a scenario where the 2nd script executes before the 1st script tag loads from the CDN and inits the jQuery object?
[1] http://html5boilerplate.com/ http://html5boilerplate.com/
- ryanpetrich 15y agoScripts are loaded serially if included in the HTML or inserted via document.write, but not if inserted via DOM methods. The defer and async attributes can be used to opt out of this behaviour on certain browsers.
- rkalla 15y agoGot it, thank you Ryan.