8

Why using <link href="//something.com/style.css" rel="stylesheet"> instead of using http:// or https:// before the domain name?

If we use href=// does it changes with link? Like while in SSL mode will it automatically be changed to https://?

unor
  • 92,415
  • 26
  • 211
  • 360
DasCodes
  • 334
  • 1
  • 4
  • 15
  • http://stackoverflow.com/questions/4978235/absolute-urls-omitting-the-protocol-scheme-in-order-to-preserve-the-one-of-the – jcho360 Jan 22 '14 at 20:26

2 Answers2

9

Yes, it will use the current protocol.

i.e. if the current page is https it will access the href using https.

If http then the link is accessed over plain http.

This will prevent browser warnings if the hosting page is https and will be more secure than a plain http link.

SilverlightFox
  • 32,436
  • 11
  • 76
  • 145
  • 1
    "This will prevent browser warnings" if the destination host is also configured to correctly serve the resource via SSL. If it's not, then the behaviors range from timeouts to SSL certificate warnings to 404s. – Palpatim Jan 22 '14 at 20:29
  • This will only work if the domain of the resource supports the same protocol as the hosting page is being accessed under. – SilverlightFox Jan 22 '14 at 20:31
5

It's just as you guess: using href="//..." without specifying the URI scheme allows it to dynamically match whichever protocol was used to access the resource, for example http or https.

It's really just an example of a relative path, but one that is relative to the protocol.

Source: the IETF's URI syntax documentation, sections 3.1 (Scheme) & 4.2 (relative reference)

brittlewis12
  • 113
  • 6