html - links without http protocol

2020-01-25 00:29发布

Is there a reason we include the http / https protocol on the href attribute of links?

Would it be fine to just leave it off:

<a href="example.com">my site</a>

4条回答
萌系小妹纸
2楼-- · 2020-01-25 00:56

The inclusion of the “http:” or “https:” part is partly just a matter of tradition, partly a matter of actually specifying the protocol. If it is defaulted, the protocol of the current page is used; e.g., //www.example.com becomes http://www.example.com or https://www.example.com depending on the URL of the referring page. If a web page is saved on a local disk and then opened from there, it has no protocol (just the file: pseudo-protocol), so URLs like //www.example.com won’t work; so here’s one reason for including the “http:” or “https:” part.

Omitting also the “//” part is a completely different issue altogether, turning the URL to a relative URL that will be interpreted as relative to the current base URL.

The reason why www.example.com works when typed or pasted on a browser’s address line is that relative URLs would not make sense there (there is no base URL to relate to), so browser vendors decided to imply the “http://” prefix there.

查看更多
萌系小妹纸
3楼-- · 2020-01-25 00:57

URLs in href are not restricted to only HTTP documents. They support all the protocols supported by browsers- ftp, mailto, file etc.

Also, you can preceed URL name with '#', to link to a html id internally in the page. You can give just the name or directory path, without a protocol, which will be taken as a relative URL.

查看更多
做自己的国王
4楼-- · 2020-01-25 00:57

My solution was to trick the browser with a redirect service, such as bit.ly and goo.gl (which will be discontinued soon), in addition to others.

When the browser realizes that the url of the shortcuts is https, it automatically releases the link image, the link is released and instead displays the http image, without showing the original link.

The annoying part is that, according to the access, it will display in the panel control of your redirector, thousands of "clicks", which is actually "display".

With this experience I'm going to look for a Wordpress plugin for redirection and create my own "redirects links". So I will have https // mysite.com /id → redirect to http link.

查看更多
一纸荒年 Trace。
5楼-- · 2020-01-25 00:57

Without http, it would take the relative URL. If you have to point to an external URL, http needs to be specified, else it would try to take you a local resource.

So, it depends on where you want to point to.

查看更多
登录 后发表回答