Href link missing http(s) prefix
P粉329425839
2023-08-22 21:56:49
<p>I just created a simple HTML page. Here: example
This is its markup: </p>
<pre class="brush:php;toolbar:false;"><a href="www.google.com">www.google.com</a>
<br/>
<a href="http://www.google.com">http://www.google.com</a></pre>
<p>As you can see, it contains two links. The href of the first link does not have the 'http' prefix, and when I click on this link, the browser redirects me to the non-existent page https://fiddle.jshell.net/_display/www.google. com</code>. The href of the second link has this prefix, and the browser generates the correct URL <code>http://www.google.com/</code>. Is it possible to use href without http(s) prefix like <code>www.something.com</code>? </p>
You can omit the protocol by using
//
in front of the path. Here is an example:By using
//
, you tell the browser that this is actually a new (complete) link, rather than a relative link to the current link.It's possible, and you're doing it now. It just won't do what you want.
Think about what the browser will do when you link to this:
So when you link to this, what does it do? :
Or this? :
or? :
The browser doesn't know what you mean, it only knows what you tell it. Without the prefix, it will operate according to the standard of the current HTTP address. The prefix tells it to start from a completely new root address.
Please note that you don't need the
http:
part, you can do this:The browser will use the current protocol (
http
,https
, etc.), but//
tells it that this is a new root address.