I have the following anchor tag.
<a href="www.google.com">Google</a>
When I click on the above link the URL will get mydomain.com/www.google.com
I have to say When I set href like the following
<a href="https://google.com">Google</a>
It works and goes to google.com
How can I have an anchor tag like this : <a href="www.google.com">Google</a>
2
Answers
To ensure that the anchor tag
<a href="www.google.com">Google</a>
behaves as expected and navigates to the correct URL, you need to provide the complete URL including the protocol (e.g., "https://"😉 in thehref
attribute.If you omit the protocol, the browser will interpret the URL as a relative path instead of an absolute URL. In your case,
www.google.com
without a protocol will be treated as a relative path and appended to the current domain, resulting inmydomain.com/www.google.com
To achieve the desired behavior, you can modify the href attribute as follows:
You need to add
https://
in the beginning of anhref
attribute for anchor tags. That’s how it works and that’s what you should follow. But if you really want an alternative solution then you can try the cool trick that I have written with JavaScript.