Whenever we have a hyperlink which does not take you to the different page and its value is “#” then we generally get into this problem that – on clicking on hyperlink, page url is appended with # and focus is moved to top of the page.

Javascript Solution: 

To avoid page focus moving to top and page getting scrolled to top, we can return 0 when a anchor or hyperlink is clicked on a webpage.

Javascript Avoid Scroll To Top On Link Click

Javascript Avoid Scroll To Top On Link Click

So just change the value from “#” to “javascript:void(0)”

<a href=”javascript:void(0)”>Sample Hyperlink</a>

Now with above trick, click will happen and user will be at same place instead of scrolling to top.

You might be thinking whats the use of this ?

We can write onclick event and perform some operation or we can write javascript or jquery handler which will perform some operation.

But in any case our focus will be at the same position, instead of scrolling and getting lost.

JQuery Solution :

If you are using JQuery and want to achieve same functionality then in JQuery we can call “event.preventDefault()” method to block default behavior.

So whenever we write click event of any anchor tag / hyperlink then at the end of the function, if we write event.preventDefault() then it will do the work.

If you know any other solution then let me know, I would love to add it here.

Simple Tip : Disable Scrolling To Top On Link Clickhttp://www.prowebguru.com/wp-content/uploads/javascript-avoid-scroll-top.jpghttp://www.prowebguru.com/wp-content/uploads/javascript-avoid-scroll-top-150x150.jpg ProWebGuru Programming & Coding
Whenever we have a hyperlink which does not take you to the different page and its value is '#' then we generally get into this problem that - on clicking on hyperlink, page url is appended with # and focus is moved to top of the page. Javascript Solution:  To avoid...
Whenever we have a hyperlink which does not take you to the different page and its value is "#" then we generally get into this problem that - on clicking on hyperlink, page url is appended with # and focus is moved to top of the page.<span id="more-1329"></span> <strong>Javascript Solution: </strong> To avoid page focus moving to top and page getting scrolled to top, we can return 0 when a anchor or hyperlink is clicked on a webpage. So just change the value from "#" to "javascript:void(0)" <p dir="ltr"><strong><a href="javascript:void(0)">Sample Hyperlink</a></strong></p> <p dir="ltr">Now with above trick, click will happen and user will be at same place instead of scrolling to top.</p> <p dir="ltr">You might be thinking whats the use of this ?</p> <p dir="ltr">We can write onclick event and perform some operation or we can write javascript or jquery handler which will perform some operation.</p> <p dir="ltr">But in any case our focus will be at the same position, instead of scrolling and getting lost.</p> <p dir="ltr"><strong>JQuery Solution :</strong></p> <p dir="ltr">If you are using JQuery and want to achieve same functionality then in JQuery we can call "<strong>event.preventDefault()</strong>" method to block default behavior.</p> <p dir="ltr">So whenever we write click event of any anchor tag / hyperlink then at the end of the function, if we write event.preventDefault() then it will do the work.</p> <p dir="ltr">If you know any other solution then let me know, I would love to add it here.</p>