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

javascript - jQuery CDN is not loading on LocalHost - Stack Overflow

programmeradmin3浏览0评论

I have a jquery cdn loading from the following:

<head>
.
.
<script src="//ajax.googleapis/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

then in the body I have my source for my script

<body>
.
.<script src="app.js"></script>
</body>

This is all on local, but when I view in browser I keep getting the following errors in console:

GET file://ajax.googleapis/ajax/libs/jquery/1.9.1/jquery.min.js
Uncaught ReferenceError: $ is not defined 

I assume this is saying the jQuery function "$..." is undefined because there is an error in GET of the CDN, why would this be happening on local?

I have a jquery cdn loading from the following:

<head>
.
.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

then in the body I have my source for my script

<body>
.
.<script src="app.js"></script>
</body>

This is all on local, but when I view in browser I keep getting the following errors in console:

GET file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
Uncaught ReferenceError: $ is not defined 

I assume this is saying the jQuery function "$..." is undefined because there is an error in GET of the CDN, why would this be happening on local?

Share Improve this question asked Apr 3, 2013 at 18:25 FluxEngineFluxEngine 13.3k16 gold badges59 silver badges84 bronze badges 2
  • 1 Try seeing what is happening via a debugging too. For instance, if you are using Chrome open your inspector, to to the 'Network' tab, and refresh the page. You will be able to see if your request is failing, etc. – Nick Mitchinson Commented Apr 3, 2013 at 18:30
  • @NickMitchinson it says status:(failed) & Type:PENDING – FluxEngine Commented Apr 3, 2013 at 18:39
Add a comment  | 

3 Answers 3

Reset to default 15

You aren't actually running on localhost (http://localhost) but on the local file system (file:///path/to/whatever.html). The protocol is copied with the // link to file://ajax.googleapis.com which of course doesn't exist.

You should set up a server on your computer so that you'd have an actual localhost accessed with the http protocol. It would have other benefits as well since browsers act a bit differently also in other regards when the page is loaded directly from the file system.

Have you tried removing the "//" at the beginning of the "src" attribute and instead using "http://"? It is probably appending "localhost" to the beginning of the URL because of those slashes.

I've answered a similar question in How to use Bootstrap CDN? and, as Juhana said, the problem is the missing protocol when loading jQuery.

The protocol-less CDN works when you are using http or https, because browsers will add the prefix to the jQuery URL with it.

But when using a local html file, the missing protocol will be file: and browsers will not be able to find jQuery in something like file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js failing to load it. In that case you must add the protocol manually, which is a pitty if you finally use the page on-line because a protocol-less URL is better and more cache friendly (http://encosia.com/cripple-the-google-cdns-caching-with-a-single-character/).

发布评论

评论列表(0)

  1. 暂无评论