最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论