How to force the page scroll position to the top at page refresh in html with JavaScript?

Estimated read time 1 min read

You can force the page scroll position to the top at page refresh in HTML with JavaScript using the window.scrollTo method. Here’s an example:

window.onbeforeunload = function () {
  window.scrollTo(0, 0);
}

In this example, we use the window.onbeforeunload event to listen for when the page is about to be reloaded. When this event is triggered, we call the window.scrollTo method and pass in the 0, 0 arguments, which set the horizontal and vertical scroll positions to 0, respectively.

This will cause the page to automatically scroll to the top when the page is refreshed.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply