Home > Software design >  How to prevent browser to follow href='#'
How to prevent browser to follow href='#'

Time:01-02

Hello recently I'm facing a new problem. I used a custom html code in the middle section of my website. After implement the html my website automatically go to that section after loading. I think this is the culprit:3

<div  id="results">
            <a  href="#" id="domain">helloworld.com</a>
</div>

Here there is an hash tag that force browser to go to this particular section. That is to say I used "javascript:void(0)" instead of # but nothing improved. My question is how can I push browser to say header and not to follow that result id.

CodePudding user response:

Simple use javascript It will changes hash in url from anything to your required header

Make sure your header has the id attribute header.

window.location.hash = "header";

CodePudding user response:

You can prevent the browser to follow a link (with an assigned href) with some simple JS code:

document.getElementById("domain").onclick(e => e.preventDefault());

Where e is the Click event object.

Although a javascript:void(0) on the href property should do the trick too.

You should probably also set the link's rel property to nofollow, like this:

<a  href="#" id="domain" rel="nofollow">helloworld.com</a>
  • Related