user3869304
user3869304

Reputation: 873

How to remove "#id of a div" from address bar in html while using href?

I am making a static website in which I am using href to go to particular div/section and which is working flawlessly. But I am trying to remove id of a div from address bar "http:localhost:3000/index.html/#about" when user clicks particular link.

Index.html

<a href="#about">About</a>
<a href="contacts">Contacts</a>

<section id="about></section>

<section id="contacts"></section>

Upvotes: 1

Views: 1006

Answers (2)

user3869304
user3869304

Reputation: 873

This block of code worked for me

 $(function() {
  // Smooth Scrolling
  $('a[href*="#"]:not([href="#"])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44087

This is the simplest way to do it:

<a href="#About" id="aboutButton">About</a>

I added that ID for this event listener:

document.getElementById("aboutButton").addEventListener("click", function() {
    window.location.href = location.pathname;
)}

This will remove everything from address bar after .html

Upvotes: 1

Related Questions