How not to show #anchor in the URL?

How can I prevent #anchor from going to the URL? I am using pure JavaScript throughout my project.

I would like to see the clear URLs as: "www.exemple.com/"
I don’t want it like this: "www.exemple.com/#anchor"

<a href="#anchor">

<div id="#anchor">

30 thoughts on “How not to show #anchor in the URL?”

  1. Like this

    Also do not use id="#something" but just id="something"

    document.getElementById("nav").addEventListener("click", function(e) {
      e.preventDefault()
      const tgt = e.target;
      document.getElementById(tgt.dataset.id).scrollIntoView(true)
    })
    html {
      scroll-behavior: smooth;
    }
    <div id="nav">
      <a href="" data-id="div1">Click for 1</a>
      <a href="" data-id="div2">Click for 2</a>
    </div>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <div id="div1">Here is 1</div>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <div id="div2">Here is 2</div>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    <p>..</p>
    Reply

Leave a Comment