8

Possible Duplicate:
Is it valid to replace with // in a <script src=“…”>?

We have a site on http[s]://A.example.com/ that references some resources (JS and CSS) on http[s]://A.example.com/

It makes sense, and avoids some browser warnings, if the same scheme is used when referencing the resources as the main site A is using. Site A uses PHP, so we can programmaticly detect the current scheme (http or https) and insert that into the header of the page.

However, we have found that just using '//' instead of the scheme also works.

So site A may reference "//B.example.com/theme/main.css" without a problem in all browsers we have tried, regardless of whether we go to site A using http or https.

My question is: is this a valid URL? It seems to work, but I cannot find anyone else doing it, and I cannot find any examples of this being suggested or recommended.

Community
  • 1
  • 1
Jason
  • 4,411
  • 7
  • 40
  • 53
  • Just a note - this is always going to be in the header of a web page, so will always be in the context of a parent page. I am not typing "//example.com" into a browser and expecting it to know I want the "http" scheme. – Jason Aug 30 '12 at 17:19
  • possible duplicate of [Is it valid to replace http:// with // in a – Wesley Murch Aug 30 '12 at 17:20
  • I didn't see that - searching on different keywords. Thanks - the application and answer is spot-on. – Jason Aug 30 '12 at 18:57

1 Answers1

14

Yes. It is called a protocol-relative URL. See also Can I change all my http:// links to just //?

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 3
    Consider voting to close as a duplicate so that we don't detract attention from the many existing valuable answers. If not, please consider making your answer self-contained. – Wesley Murch Aug 30 '12 at 17:24
  • Neh it should just be closed at minimum. Perhaps even just be deleted. @WesleyMurch – PeeHaa Aug 30 '12 at 17:25
  • I say leave it.... I found this one first because it matched my search terms best. And now it'll lead me to the other one! – Brad Parks Dec 23 '14 at 13:10