简体   繁体   中英

Remember scroll position on navigating back to first page from second one

if we are having a page which is loading content by ajax on scrolling to bottom,if we click on any link on scrolling down to some position on page and navigate to next page.And when from next page,I press the button ,i want the page to go to same position from where i cliked. Please guide me how it can be done.

On navigating to second page you can send position via QueryString and get that queryString on another page using javascript and vice versa similarly.Lets say you navigated on page2 like 'page2.html?position=44' Here is the code to get queryString:

JavaScript:

function getQueryString(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

Usage: var position = getQueryString('position');

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM