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
6 Answers
Reset to default 8They 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
版权声明:本文标题:javascript - What's the reason to include scripts with two different calls? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741693116a2392847.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论