I use HTML5 boilerplate and jQuery is declared twice in the HTML page like this:
If your question is "why is the transfer protocol not specified?," then the answer is "it doesn't have to be specified." This lets you use the same script reference regardless of whether the connection is using a secure socket or not without having your users receiving warnings about encrypted/unencrypted content.
If the question was "what is this line doing?: window.jQuery || document.write('</script>')," then the answer is that we are using a (more or less) ternary statement to test the jQuery object, which will evaluate to a "false" value if the jQuery library was not loaded, and if so, this test will trigger the second half of the statement, resulting in the local jQuery being loaded.
HTH.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
This will attempt to load the protocol-less version of the jQuery library
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>
This will load the local version of the jQuery library if the google-hosted version was not loaded properly (unreachable, site is down, etc), hence the window.jQuery
check. If window.jQuery
is not true
then it will execute the document.write
Loading jQuery from the Google CDN can be much faster than loading it from your local server and it can be cached so the user might already have a cached copy of it from another website.
The check is make sure that it got loaded, otherwise if it failed, load it from the local server.
Yes, it's checking if jQuery is loaded or not, if not then loading it from own server.
//
only is used to make it compatible with both HTTP and HTTPS.
The reason is failback. The first line of code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
Pull the jQuery library from the Google CDN as you said. Then this next line:
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>
Will verify that jQuery library was loaded (from the Google CDN), if not, then retrieve a local copy of jQuery.
They reason html5 Boilerplate includes the script that way is because it attempts to "load jQuery library from local server if it's not reachable from Google CDN." =)