1

Possible Duplicate:
load jQuery from external source if not loaded

How can I set up a backup if jquery doesn't load?

I've found and tried this:

if (typeof jQuery == 'undefined') {
    alert('jquery didn't load !');
    loadScript("/here/jquery.min.js");
    .... // this is where i'd want to say load from '/here/jquery.js'
}

but I don't know how to call the src of a script within a script.

Community
  • 1
  • 1
d-_-b
  • 21,536
  • 40
  • 150
  • 256
  • try the answer from this question: http://stackoverflow.com/questions/5415821/jquery-from-cdn – red-X Sep 22 '12 at 21:42

4 Answers4

4

This is fairly common:

<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.2.js"%3E%3C/script%3E'))</script>
Johan
  • 35,120
  • 54
  • 178
  • 293
2

Look here How can I load jQuery if it is not already loaded? or load jQuery from external source if not loaded

if(typeof jQuery=='undefined') {
    var headTag = document.getElementsByTagName("head")[0];
    var jqTag = document.createElement('script');
    jqTag.type = 'text/javascript';
    jqTag.src = 'jquery.js';
    jqTag.onload = myJQueryCode;
    headTag.appendChild(jqTag);
}
Community
  • 1
  • 1
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
0

put a div in regular HTML that says jquery didn't load , then hide it with jquery

<div class="jqueryError">
  <a> jquery did not load </a>
</div>

$(function(){
  $('jqueryError').hide();
});
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
0

I assume you want to load jquery if you are connected to the internet?

You could try this:

function loadJQuery() {
  if (navigator.onLine(connected)) {
    google.load("jquery", "1.4.1");
  } else {
    document.getElementById('jquery_loader').src = '/javascripts/jquery.js';
  }
 }

Remember though, if you are testing offline you could use this, but it would be useless on a website, because everyone will be connected to the internet in order to visit your website.

Randy
  • 9,419
  • 5
  • 39
  • 56