Prevent scroll jump during refreshing a long HTML page

html, javascript

Solution

How about use html5 localStorage function.

window.addEventListener('scroll', function () {
    localStorage.scrollX = window.scrollX;
    localStorage.scrollY = window.scrollY;
})
window.addEventListener('load',function () {
    window.scrollTo(localStorage.scrollX || 0, localStorage.scrollY || 0);
})

Check it on http://jsfiddle.net/g5NKG/10/show/

Problem

On refreshing a long HTML page, the scroll position is initialized to the top and then jumped to the last scroll position. Is there a way to stop this scroll jump behavior on refresh and just initialize scroll position to last scroll position?

Original source

Related problems