4

I have read in several places how to fallback on a local copy of the jQuery library should the link hosted by either google or microsoft or other fail.

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined')
{
document.write(unescape("%3Cscript src='/Scripts/jquery-1.3.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>

My application works within an intranet environment however and occasionally the external jQuery link doesn't so much fail but takes a long time to load (due to external internet connection issues).

I'm wondering if there is a way to not only use such a fallback but set a timeout for the CDN link so that if the link takes a certain amount of time it should fail and call on the fallback.

Something like:

if(timetoloadjquery > n) {
    Use fallback local jQuery library.
}

Perhaps some kind of loop that checks if the jQuery is defined and if after so many iterations it is not....do something else?

Thanks for the help.

Barbs
  • 1,115
  • 2
  • 16
  • 30
  • possible duplicate of [Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail](http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-go) – Michael Dunlap Dec 11 '13 at 22:04
  • (I never understood the CDN thing. Extra DNS lookups, extra downloads and still requires fallbacks.) – Rudie Dec 11 '13 at 22:15
  • A ` – Rudie Dec 11 '13 at 22:16
  • What about `onerror=` and `onload=` in the ` – Rudie Dec 11 '13 at 22:17
  • @Rudie: Browsers download 3 or more (depending on browser) files simultaneously, how do they do that if each script source is blocking? – Nope Dec 11 '13 at 22:23
  • 1
    The execution is in order, after download. The next script block will only be executed after the previous has been downloaded & executed (or failed). – Rudie Dec 11 '13 at 22:32
  • @DigitalD: The difference is that thread calls the fallback after the CDN link fails. In my case the CDN link can take a long time to timeout. I want to speed things up by failing the CDN script load after say 5 seconds because I don't want my page to wait for a long script download. – Barbs Dec 11 '13 at 23:25

1 Answers1

1

This may help you. After 5-seconds have passed, Javascript checks if jQuery is available, if not, then loads the library from local server.

1. With a timer

<script>
setTimeout(function() {
  if(window.jQuery) return;
  var n = document.getElementsByTagName("script")[0];
  n.parentNode.insertBefore(document.createElement("script"), n).src = "assets/jQuery/jquery-1.7.2.min.js";
}, 5000);
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>

2. This one doesn't have a timer, it loads a local version if the CDN version fails.

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js"></script>
<script type="text/javascript">window.jQuery || document.write("<script type='text/javascript' src='js/jquery-1.8.3.min.js'>\x3C/script>")</script>
TheCarver
  • 19,391
  • 25
  • 99
  • 149
  • 1
    @Barbs The issue with the first version (with timer) is that the slow loading library will continue to load. It won't abort it. – TheCarver Dec 11 '13 at 23:48
  • thanks I might just have to give up and host the library locally. – Barbs Dec 12 '13 at 00:59
  • OK, let's assume jQuery is loaded twice. Will it somehow affect presentation or performance? The biggest question is whether the page will show up BEFORE jQuery is loaded the second time. – Megan Caithlyn Jul 05 '15 at 16:03