Prevent Ajax website from jumping back to top after back-button-press

I am currently working on a website with ajax support. Navigating through website and the browser's history works great, but there is one problem I cannot solve.

When the user presses the back button, the last page appears again. But although I set the vertical scroll position to the previous one via popstate event, it jumps back to top again...

function loadContent(nextContent, nextUrl, newPage = true) {
    var currUrl = window.location.pathname;
        currUrl = currUrl.slice(7,currUrl.length);
    var scrollY = window.scrollY;
    var prevContent = document.getElementById("page").innerHTML;

    document.getElementById("page").innerHTML = nextContent;

    if (newPage) {

        // overwrite current history entry
            history.replaceState({ content: prevContent, url: currUrl, scrollY: scrollY }, null, currUrl);


        // write new entry
            history.pushState({ content: nextContent, url: nextUrl, scrollY: 0 }, null, nextUrl);

    } else {
        // scroll to previous scroll position

window.onpopstate = function(e) {
    loadContent(e.state.content, e.state.url, false);
    // page scrollY will be set in load, after page content was added

I know that the code works so far because earlier I've put an alert after window.scrollTo() and during the freeze I saw, that the page is on the correct position. But after continuing it jumped to the top. Is there maybe another (default) event, that scrolls to the top in the background?

Thanks for help



I fixed the problem now by changing the onpopstate function to the following:

window.onpopstate = function(e) {
    loadContent(e.state.content, e.state.url, false);

    // as previous window.scrollTo() have no effect, do it within a few millisecs again
    setTimeout(function() {
    }, 5);

Not really beautiful, but it works. If someone knows a clean fix or the reason why this effect happens, please share your knowledge!


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.