Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using href="// instead of href="http:// in HTML? [duplicate]

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://?

like image 395
DasCodes Avatar asked Jan 22 '14 20:01

DasCodes


2 Answers

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.

like image 151
SilverlightFox Avatar answered Oct 15 '22 05:10

SilverlightFox


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)

like image 38
brittlewis12 Avatar answered Oct 15 '22 05:10

brittlewis12