Week 1: Noodle 1

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

WARNING: This noodle uses some jQuery/Javascript you will probably not be familiar with at the moment. That's fine - you're not expected to understand this code yet: Just look at what it does and you can come back to it later when your understanding grows.

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


See the code:

<!-- Link to externally hosted jQuery framework (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
   if (typeof jQuery == 'undefined') {
      // If jQuery isn't loaded, write a link to the local backup version in the js folder
      document.write("<script type=\"text/javascript\" src=\"js/jquery-1.10.2.js\"><\/script>");
   }
</script>
	
<!-- This script checks if jQuery is loaded (both above methods *might* fail) and alerts us -->
<!-- This is standard Javascript. Obviously we can't use jQuery to test if jQuery loaded! -->
<script type="text/javascript">
   window.onload = function() {
      if (window.jQuery) {  
         // alert the user that jQuery has loaded  
         alert("jQuery loaded successfully!");
      } else {
         // alert the user that jQuery has not loaded
         alert("jQuery not found!");
      }
   }
</script>