overexchange overexchange - 4 years ago 99
Javascript Question

Is it mandatory to mention protocol to load jQuery?

Below code,

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
</script>
<script type="text/javascript">
console.log(jQuery);
</script>





works fine in firefox browser after
src
is modified to
"http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"
(remote file) or
"../js/jquery.min.js"
(local file)

Otherwise, dev console gives
Reference error: jQuery is not defined


I would like to test the code with remote library but not local

How do I understand this problem?

Answer Source

As mentioned by Mosh Feu, if you run a file locally, without a webserver, you cannot use protocol relative paths to load jQuery. That's because it is trying to find a local reference: file://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js.

Well, you could if you have jQuery locally in a folder /some/where/jQuery.js and you reference it with <script src="//some/where/jQuery.js>

So yes, if you're running pages from the disk directly, you must specify the protocol if you want jQuery from a CDN. See the first comment on http://www.paulirish.com/2010/the-protocol-relative-url/

Save yourself some trouble, install a local web server.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download