admin管理员组

文章数量:1302407

I use HTML5 boilerplate and jQuery is declared twice in the HTML page like this:

<script src="//ajax.googleapis/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>

What's the reason behind including the JavaScript files this way?

It seems to be the only reason is to load jQuery library from local server if it's not reachable from Google CDN.

I use HTML5 boilerplate and jQuery is declared twice in the HTML page like this:

<script src="//ajax.googleapis./ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.2.min.js"><\/script>')</script>

What's the reason behind including the JavaScript files this way?

It seems to be the only reason is to load jQuery library from local server if it's not reachable from Google CDN.

Share Improve this question edited Apr 27, 2016 at 23:36 random 9,95510 gold badges69 silver badges84 bronze badges asked Jan 3, 2012 at 18:13 Sergei BasharovSergei Basharov 53.9k78 gold badges207 silver badges352 bronze badges 2
  • 8 I'm not quite sure what you're asking. You've given the answer in your question... – lonesomeday Commented Jan 3, 2012 at 18:15
  • works if you don't have an internet connection – Neil McGuigan Commented Jan 3, 2012 at 18:29
Add a ment  | 

6 Answers 6

Reset to default 8

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." =)

<script src="//ajax.googleapis./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 patible with both HTTP and HTTPS.

The reason is failback. The first line of code

<script src="//ajax.googleapis./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.

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.

本文标签: javascriptWhat39s the reason to include scripts with two different callsStack Overflow