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.
+ There are no comments
Add yours