Loading jquery from google doesn't work (for me)

前端 未结 6 633
盖世英雄少女心
盖世英雄少女心 2021-01-16 20:25

Ah the wretched noob I am, the following html document doesn\'t alert anyone of my cry for help. Anyone know why?





        
相关标签:
6条回答
  • 2021-01-16 21:03

    You are missing an http: in front of the url in the src attribute of the <script> tag:

    <html>
    <head>
    <script type="text/javascript"
     src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        alert('Somebody please help me.');
      });
    </script>
    </head>
    <body>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-16 21:04

    That same syntax is used in the HTML5 Boilerplate:

    <!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if necessary -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js"></script>
    <script>window.jQuery || document.write("<script src='js/libs/jquery-1.5.2.min.js'>\x3C/script>")</script>
    
    0 讨论(0)
  • 2021-01-16 21:04

    The source is jacked up. Use

    src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"
    
    0 讨论(0)
  • 2021-01-16 21:07

    This works for me:

    <html>
    <head>
    <script type="text/javascript"
     src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        alert('Somebody please help me.');
      });
    </script>
    </head>
    <body>
    </body>
    </html>
    

    Just fixed the src in the script tag.

    Edit: Actually, the original syntax would work fine if you load the page in a non-local context. Leaving out the protocol implies that the 'current' protocol would be used depending on whether resources are loaded over http or https. Loading it locally implies that the script is loaded from file:///ajax.googleapis.com/...., which obviously won't resolve to anything. See here for more information. Thanks to @PetrolMan for pointing to the HTML 5 boiler plate site.

    0 讨论(0)
  • 2021-01-16 21:15

    It's

    http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
    

    not

    //ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
    
    0 讨论(0)
  • 2021-01-16 21:28

    Possible values for the Src Attribute are

    •An absolute URL - points to another web site (like src="http://www.example.com/example.js") •A relative URL - points to a file within a web site (like src="/scripts/example.js")

    So you should have your URL as http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js

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