What's the reason to include scripts with two different calls?

后端 未结 6 1898
小蘑菇
小蘑菇 2021-01-05 05:15

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



        
相关标签:
6条回答
  • 2021-01-05 05:26

    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.

    0 讨论(0)
  • 2021-01-05 05:28
    <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

    0 讨论(0)
  • 2021-01-05 05:30

    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.

    0 讨论(0)
  • 2021-01-05 05:30

    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.

    0 讨论(0)
  • 2021-01-05 05:33

    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.

    0 讨论(0)
  • 2021-01-05 05:38

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

    0 讨论(0)
提交回复
热议问题