Reputation: 43
I have an index.html
file in the desktop and I would like to get jQuery library with these options:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js></script>
But looks like it doesn't work on local file.
What went wrong?
Upvotes: 2
Views: 135
Reputation: 15711
The reason why it is not working properply is because starting a URL with //
will use the "current" protocol. On internet, you access resources with the HTTP
or HTTPS
protocols. But when showing a file from the local hard drive, the protocol used is FILE
so instead of translating to the wanted http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js
translates to file://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js
So your option here is to either load the script using javascript like the following, or hard code a protocol.
<script>
var p = 'http';
if (document.location.protocol == "https:"){
p+='s';
}
var z = document.createElement("script");
z.type = "text/javascript";
z.src = p + "://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(z, s);
</script>
This will look at the current protocol so it uses the right one.
PS: I've used the adzerk code from SO's source and modified it... I know that Google analytics uses the same kind of snippet.
Upvotes: 2