15

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

5
  • Did you try to .scrollTop(0) inside a setTimeout? Commented Mar 12, 2013 at 7:50
  • scrollTop-ing late enough is the only thing I can think of. Say, 50ms after the page load. It seems hacky though. Commented Mar 12, 2013 at 7:51
  • @Jan it is hacky, on the other hand the whole theme is a bit hacky. Commented Mar 12, 2013 at 7:52
  • you might need to put it in the documents onready, in which case it should work Commented Mar 12, 2013 at 8:19
  • Possible duplicate of Disable brower's auto scroll after a page refresh? Commented Aug 18, 2017 at 14:01

5 Answers 5

35

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'; } 
Sign up to request clarification or add additional context in comments.

4 Comments

This works, but required some extra tweaking for me.
this is so simple and it took 2 days to find out about scrollRestoration... thank you!
Works great! And all the major browsers now support it caniuse.com/?search=scrollRestoration
this is the only correct solution, as the other ones will actually FORCE the browser to autoscroll to the position 0 after window is refreshed. and you might not want to do this, but instead scroll to your own computed position
3

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.

6 Comments

If I remember correctly, do window.scrollTo(0) on page load, and Chrome overrides your scroll.
I meant even window.onload is too soon for window.scrollTo. Several miliseconds after window.onload should do, however.
If I want the page scroll to left, not the top, what should I do?
Seconding @JanDvorak's findings. A setTimeout within window.onload works, but by then the window has already scrolled.
window.scrollTo(0) throws "cannot convert to dictionary" in Chrome and Firefox. Seems that you need to specify x and y like so: window.scrollTo(0, 0)
|
2

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.

1 Comment

body hidden is not work; change to window.scrollTo(0, 0); it will done.
0

This worked for me :

JS Way:

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

Jquery way:

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

Comments

0

Another option may be adding and removing overflow:hidden; to body html or element

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.