javascript - Loading jquery from google doesn't work (for me) - Stack Overflow

Ah the wretched noob I am, the following html document doesn't alert anyone of my cry for help.A

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

<html>
<head>
<script type="text/javascript"
 src="//ajax.googleapis/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>

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

<html>
<head>
<script type="text/javascript"
 src="//ajax.googleapis./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>
Share Improve this question asked May 3, 2011 at 21:29 JnBrymnJnBrymn 25.4k31 gold badges110 silver badges152 bronze badges 1
  • Which browser are you using and where is this page located (i.e. is it a standalone .htm file you're opening in the browser, testing on localhost, Internet hosted?)? – Dave Ward Commented May 3, 2011 at 22:02
Add a ment  | 

6 Answers 6

Reset to default 6

This works for me:

<html>
<head>
<script type="text/javascript"
 src="http://ajax.googleapis./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./...., which obviously won't resolve to anything. See here for more information. Thanks to @PetrolMan for pointing to the HTML 5 boiler plate site.

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./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>

It's

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

not

//ajax.googleapis./ajax/libs/jquery/1.5.1/jquery.min.js

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./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>

The source is jacked up. Use

src="http://ajax.googleapis./ajax/libs/jquery/1.5.1/jquery.min.js"

Possible values for the Src Attribute are

•An absolute URL - points to another web site (like src="http://www.example./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./ajax/libs/jquery/1.5.1/jquery.min.js

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742383961a4433677.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信