10

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?

mwcz
  • 8,949
  • 10
  • 42
  • 63
  • 2
    There are a lot of answers, but no actual references to the standard which specifies this behavior. I think this sounds useful, but I wouldn't want to use it if it's just an implementation quirk. – Teddy Jan 07 '10 at 05:01
  • 1
    @Teddy It is apparently called "Class X7.45" and is specified in RFC 1808 http://www.w3.org/Addressing/rfc1808.txt – mwcz Jan 08 '10 at 15:16
  • See also http://stackoverflow.com/questions/11881054/is-a-url-starting-with-valid – koppor Dec 15 '13 at 17:22
  • For future readers, it is secs. 2.2 and 2.4.5 of @mwcz's link. – cxw Feb 25 '17 at 12:07

4 Answers4

9

It will use the protocol you're already using. Useful for sites with both https and http versions.

So if you're on https://www.domain.tld/file.js the script will be https://www.domain.tld/file.js.

If you're on http://www.domain.tld/ the script will be http://www.domain.tld/file.js.

Brian McKenna
  • 45,528
  • 6
  • 61
  • 60
3

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

D.C.
  • 15,340
  • 19
  • 71
  • 102
1

The purpose is that the scheme (ie. http or https) 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 via http or https.

Asaph
  • 159,146
  • 25
  • 197
  • 199
1

The purpose is to "use the same protocol as in the current URL" -- presumably (?) useful if the page can be reached both as http: and https: (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).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395