-1

I have these 3 following <a> tags that point to different hrefs. 1 and 2 work fine and open https://example.com and http://example.com in a new tab, but the 3rd one open the same URL as the one I'm currently on and just appends example.com to the end

Why is this happening?

<a href="https://example.com" target="_blank">Open</a>
<a href="http://example.com" target="_blank">Open</a>
<a href="example.com" target="_blank">Open</a>
Onyx
  • 5,186
  • 8
  • 39
  • 86

4 Answers4

2

The third one is a link to the page named example.com on the current server.

Just because it has a .com extension does not mean it is magically a domain name. It's like that by design and by definition.

The first piece of text (until the first / after it, if any) is treated as a domain name if and only if there is // or protocol:// at the beginning. And the text is treated as a (sub)page if there isn't.

Peter B
  • 22,460
  • 5
  • 32
  • 69
1

Looking at https://www.w3schools.com/tags/att_a_href.asp it seems that you are using a relative URL which assumes that you are navigating to a page that exists in the same website. This is why it tacks on the href attribute to the end of the URL.

Weagertron
  • 161
  • 2
  • 12
1

It's because without the protocol (http:// https:// ftp:// etc) the path is treated as relative to current domain. https://www.w3schools.com/html/html_filepaths.asp

vanowm
  • 9,466
  • 2
  • 21
  • 37
-1

This is called relative path.

Try this out instead Open