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

时光总嘲笑我的痴心妄想 提交于 2019-12-07 01:09:59

问题


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


回答1:


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.




回答2:


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)



来源:https://stackoverflow.com/questions/21293029/why-using-href-instead-of-href-http-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!