I recently ran across a website that had some interesting styling on a select element. I went to investigate and found this (names changed to protect the innocent):
<script type="text/javascript" src="//www.domain.tld/file.js"></script>
It works despite HTTP:
being omitted. What is the purpose of leaving off the protocol?
The purpose is to "use the same protocol as in the current URL" -- presumably (?) useful if the page can be reached both as
http:
andhttps:
(I have a hard time thinking of other protocols yet that it might be useful for, and even this one is not a clear-cut use case).i believe this is short hand for a relative path to the protocol. So it should use the same protocol as is being used for that session. e.g if you grabbed that page with http, then this url is relative to http protocol
It will use the protocol you're already using. Useful for sites with both
https
andhttp
versions.So if you're on
https://www.domain.tld/file.js
the script will behttps://www.domain.tld/file.js
.If you're on
http://www.domain.tld/
the script will behttp://www.domain.tld/file.js
.The purpose is that the scheme (ie.
http
orhttps
) can be determined relative to the containing page. This is useful if you have a common piece of code included in multiple pages that can be served viahttp
orhttps
.