1

I have an HTML page which calls a few javascript libraries using the "script" tag.

One of these is actually provided by another server which occasionally crashes and thus reports a "STATUS 404 NOT FOUND".

The advantage of using this server is that it is faster than mine and loads the libary in about 200 miliseconds whereas when it is on my server is takes about 2 seconds. However, I would like to keep a copy on my server and only load it when the browser reports a Status 404.

But how do I program that in javascript?

function LoadLib(){

if (server status == 404)
    {load library 1} 
else
    {load library 2}
}

Something along these lines?

Anyone done this before?

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
Robert Buckley
  • 11,196
  • 6
  • 24
  • 25
  • 1
    possible duplicate of [Check if file exists but prevent 404 error in console from showing up](http://stackoverflow.com/questions/7035466/check-if-file-exists-but-prevent-404-error-in-console-from-showing-up) – fyr Feb 09 '12 at 06:47
  • @fyr: I don't think this is really a duplicate of that 'prevent 404 error ... from showing' question, since this is about loading JavaScript from a CDN with a fallback to a different URL if it doesn't load from the first, which is very different than pre-checking a link before redirecting the user there. – Jon Adams Feb 17 '12 at 18:49
  • Helpful Google query that returns several results. They are focused on Google CDN usage, but could easily be modified to work for any CDN: https://www.google.com/search?q=google+cdn+fallback – Jon Adams Feb 17 '12 at 18:53

1 Answers1

1

This Fallback for CDN hosted jQuery post had a decent solution.

This example uses jQuery Microsoft CDNs, so change the src attributes and the object detection as appropriate for your script.

It also assumes the script you are loading creates some global variable you can check for existence to make sure it loaded and ran.

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

Note: This isn't the only option. You could also perform an AJAX request and read it's response, but that is more complicated code then the above solution.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
  • Another problem with Ajax requests is that they can lead to worse error messages in some browsers, since the library code must be run under eval. – hugomg Feb 17 '12 at 18:58
  • @missingno: I was thinking you would use the AJAX request to check for a 404, then if not (meaning it is valid) inject the script tag via DOM insertion or the above `document.write` method. It would trigger another HTTP request that way, but if you caching is setup correctly, then it can load the script from the browser client cache the second time so the hit for the extra request would be negligible. – Jon Adams Feb 17 '12 at 19:06