2

I have an index.html file in the desktop and I would like to get jQuery library with these options:

  • I don't want to upload index.html file to server
  • I don't want to add protocol for get jquery script.
  • The script must be retrieved from jQuery CDN (or Google) server and not from the local file.
    I tried this way: <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?

Sergej
  • 1,082
  • 11
  • 27
InBug
  • 43
  • 1
  • 9
  • If you are running it using an Apache server (for instance) it should work fine, but if you are just running it as a local file it may represent a security issue if that, or any old JavaScript file loaded. – Djave Aug 25 '15 at 13:52
  • 2
    *"I don't whant to add protocol for get jquery script."* <- this is what you do wring. If you don't specify the protocol it will use the one you are using to open the file / page, which is probable `file:`. You could run a local server though. – Felix Kling Aug 25 '15 at 13:52
  • "I don't whant to upload index.html file to server", "I don't whant to add protocol for get jquery script." why not? you've said two things that you know will work but you don't want to do them? so not really sure what answer you expect to get – atmd Aug 25 '15 at 13:56
  • if its just for testing try this: in the dir with the index.html do `php -S localhost:8000` or `python2 -m SimpleHTTPServer` and access your testfile via `localhost:8000` (that is if you have php or python installed) – birdspider Aug 25 '15 at 13:57
  • I get this job from my boss for figured out how can this possible with this options, but I can't find out. I can't use any server – InBug Aug 25 '15 at 14:00
  • well then just download the `jquery.js` (1.4.2 is way old, btw. (February 2010)) and put it in your dir, include with ` – birdspider Aug 25 '15 at 14:20
  • See the question, I also mention this option on list. My boss sayed: "Get script from the server, becouse this is latest (yes, he also sayed change the url to "latest")" – InBug Aug 25 '15 at 14:22

1 Answers1

2

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.

Salketer
  • 14,263
  • 2
  • 30
  • 58