JQuery CDN not loading on LocalHost

I have a jqery cdn download from the following:

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

then in the body there is my source for my script

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

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

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

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

+4
source share
3 answers

In fact, you are not working on localhost ( http://localhost ), but on the local file system ( file:///path/to/whatever.html ). The protocol is copied using the link // to file://ajax.googleapis.com , which, of course, does not exist.

You must configure the server on your computer in order to access the actual local host using the http protocol. This will have other advantages, as browsers act differently and in other respects when the page is loaded directly from the file system.

+14
source

Did you try to remove "//" at the beginning of the "src" attribute and use "http: //" instead? This is probably adding "localhost" to the beginning of the url because of these slashes.

+2
source

I answered a similar question in How to use Bootstrap CDN? , and as Johan said, the problem is the lack of a protocol when loading jQuery.

The CDN protocol does not work when you use http or https, because browsers will add a 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 , not loading it. In this case, you must add the protocol manually, which is useless if you finally use the page online because the URL without the protocol is better and more cache-safe ( http://encosia.com/cripple-the- google-cdns-caching-with-a-single-character / ).

0
source

All Articles