Check jQuery Loads (fallback)

Load alternate version of jQuery: Linking out to a jQuery library on a hosted server is all very well, but what if the link to the external host fails to load? We can write a script to check if the remotely hosted version of jQuery loaded, then link to a local fallback version of the library if it didn't.

jQuery loaded from a local source when the CDN link failed.

Notes:

The basic code structures used in this code example are found in most scripting/programming languages.


See the code:

<!-- Link to externally hosted jQuery library (broken deliberately...) -->
<script type="text/javascript" src="http://codexxxx.jquery.com/jquery-latest.min.js"></script>
	
<!-- This script checks if jQuery loaded successfully from the remote server. If not, it loads a 
pre-downloaded backup version stored in the site structure. -->
<script type="text/javascript">
   // Check if jQuery exists. The jQuery object will be undefined if jQuery hasn't loaded.
   if (typeof jQuery == 'undefined') {
      // If jQuery isn't loaded, write a script to load the local backup version found in the js folder
      document.write("<script type=\"text/javascript\" src=\"js/jquery-1.10.2.js\"><\/script>");
   }
</script>