5

We generally check if jQuery is loaded from CDN or not and fallback to local version if it didnt.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js"><\/script>')</script>

I am using twitter bootstrap, and it loads the html 5 shim from the googlecode copy.

<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>

Is there any way I can fallback to local version if it does not load from googlecode

Or am I doing something wrong. Shouldnt be I checking this ?

Jashwant
  • 28,410
  • 16
  • 70
  • 105

4 Answers4

11
<script>window.html5 || document.write('<script src="js/libs/your-local-version.js"><\/script>')</script>
  • I'll use the local version only but your reply is definitely the answer to question. It works ! – Jashwant Jul 01 '12 at 13:03
  • 2
    Please don't use document.write... The speculative parser can't load the script inserted by it and you'll have to wait for the code to execute and the JS inserted by it to load before other DOM elements below it can be rendered see http://www.stevesouders.com/blog/2012/04/10/dont-docwrite-scripts/ – Andy Davies Jul 01 '12 at 18:16
4

I wouldn't bother falling back with the jQuery (long discussion and can of worms I'd prefer not to open), but that's up to you.

I do think that loading the html5.js from googlecode is going to cause you more problems though, and you should host it locally. Why? Because you're referencing code directly out of the trunk of an SVN tree. That code can/will change on you, which will put your work into an untested state. You could reference a specific revision from Googlecode... or really, just host it yourself.

John Green
  • 13,241
  • 3
  • 29
  • 51
4

Host the shiv locally, there are many issues using it from Google (not least the DNS look up etc) for more see - http://zoompf.com/blog/2012/05/html5shiv-and-serving-content-from-code-repositories

As for that jQuery callback code I wouldn't bother, the user will think the page is broken long before the callback fires.

If you let me know the URL of your page I'll demonstrate.

Andy Davies
  • 5,794
  • 2
  • 26
  • 21
  • What a nice piece of info. +1. I have to mark Vladimir's answer because that correctly answers the question asked. But yes, I am not going to use google's version for sure. – Jashwant Jul 01 '12 at 13:01
  • Trouble is Vladimir's code contains bad practice from the performance perspective :-( – Andy Davies Jul 01 '12 at 18:17
  • Yes, I am not going to use Vladimir's code. I am with your suggestion. Now, I am not going to use even the cdn jQuery. `But his reply was answer to my question` – Jashwant Jul 01 '12 at 18:22
1

When using html5shim, i would recommend loading it locally in the first place. On their website they have recently changed the wording to make you download the file instead of hotlink it from GitHub, which could host untested code.

472084
  • 17,666
  • 10
  • 63
  • 81