DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Chara has posted 50 posts at DZone. View Full User Profile

jQuery Loading Fallback

08.20.2013
| 3282 views |
  • submit to reddit
<!-- Grab Google CDN's jQuery and load local version if necessary -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"><\/script>')</script>

 Nowadays majority of sites uses the jQuery JavaScript library. A lot of them also make use of Google's hosted version of the library from some reasons: faster loading, better cross site caching etc. However, what if there is ever a problem and jQuery is not loaded from Google? For example when you work on localhost and there is no Internet connection? This simple snippet will help you. It basically checks if jQuery is loaded from Google. If not, then it loads it locally from our own version of jQuery.

Comments

John Doe replied on Mon, 2013/10/21 - 2:17pm

 There should not be "http:" in ..scr1pt src="http://ajax.go ... <--