Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between http:// and //

I noticed CDNs use // in the beginning of the source of the script.

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

This doesn't work locally (obviously), so I add http: before //

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  

So... why do they put // in the beginning instead of http://? what is the significance? Is it some sort of DNS prefetch?

Also, is the // useless if the script element is at the bottom of the page?

https://developer.mozilla.org/en-US/docs/Web/HTTP/Controlling_DNS_prefetching

like image 561
Matthew Avatar asked Dec 25 '22 05:12

Matthew


1 Answers

// is a relative protocol indicator. It will load over whatever protocol is currently being used. If you're loading the page over http:, it will load the resource over http:. If you're loading it over https:, it will be loaded over https:. This is important because pages loaded over HTTPS should also load their resources over HTTPS for security purposes.

The reason why it doesn't work locally is because locally, you're using the file: protocol (obviously), so it's going to try to load the resource over file:, which won't work because it's a URL, not a file path to a resource in your directory.

You might also want to take a look at this page describing its usage (tips, common pitfalls, etc.) and the protocol-relative tag.

like image 64
AstroCB Avatar answered Dec 28 '22 07:12

AstroCB