hh54188
hh54188

Reputation: 15626

How to let the chrome forget the page scroll position?

For instance, I open a web page, and scroll down to some position,

then I refresh the chrome browser, the browser could scroll the previous position again

How can I use javascript or css to let the browser to forget the scroll?

I have try $(window).scrollTop(0), but it doesn't work

Upvotes: 15

Views: 13585

Answers (4)

Harsha Vardhan Chakka
Harsha Vardhan Chakka

Reputation: 1461

This worked for me :

JS Way:

window.addEventListener('unload', function(e){
window.scrollTo(0, 0)
});

Jquery way:

$(window).load(function() {
 $(window).scrollTop(0);
});

Upvotes: 0

Brian Zhang
Brian Zhang

Reputation: 471

It is resolved in the question below.

Disable brower's auto scroll after a page refresh?

// bypass auto scrolling.
if ('scrollRestoration' in history) {
  history.scrollRestoration = 'manual';
}

Upvotes: 35

Walex
Walex

Reputation: 79

Here is a clean way of getting this done.

window.addEventListener('unload', function(e){
   document.body.style.display = 'none';
});

By simply setting the body display to 'none' you don't have to worry about a flash of the browser scrolling to the top of the page before it is unloaded and the scroll position will automatically be reset to 0.

Upvotes: 2

Afshin Mehrabani
Afshin Mehrabani

Reputation: 34959

A simple way that I can say is to run this code on page load:

document.location = "#";

OR

window.scrollTo(0);

It set the browser viewport to the top of the page, again.

Upvotes: 3

Related Questions