0

I was wondering if its possible to prevent the empty href link to scroll up on top of the page when that link is clicked.

For instance I have:

<a href="#">Hello world</a>

If the "Hello world" link is lets say on middle of the page and I click it (if its in this form like i stated above), when I click this link, my url would look like this:

myurl.com/#

However, what happens is that each time this link is clicked as empty href tag, my page scrolls up to top, and I don't want that to happen, it's very annoying and I wanna remove that. Is there any way for me to do that via JavaScript or jQuery?

Vahid Farahmandian
  • 6,081
  • 7
  • 42
  • 62
perkes456
  • 1,163
  • 4
  • 25
  • 49

1 Answers1

0

In jQuery, you could prevent the default action of the click event like this:

$("a[href='#']").click(function(e) {
    e.preventDefault();
});

That way the URL won't change if you click on a link that has # as the href attribute, so there will be no scrolling.

benface
  • 687
  • 9
  • 19