Is there a way to implement MaintainScrollPositionOnPostBack functionality in jQuery

ASP.NET 2.0 has the following MaintainScrollPositionOnPostBack property, and it can be used to preserve the browser position in the response message. Can this be achieved using jQuery, and if so, how? I read several articles mentioning that MaintainScrollPositionOnPostBack does not work in some browsers like Chrome / Safari etc.

+3
source share
1 answer

Create a hidden ASP input field to store the position through the postback, pass the ClientID of this field to the code below:

// client id of the hidden input field
var hiddenInputId = '<%= _myHiddenInputField.ClientID %>';

// store the current scroll position into the input
function storeScrollPosition(){
    $('#'+hiddenInputId)[0].value = scrollPosition();
}

// load the value out of the input and scroll the page
function loadScrollPosition(){
    var curPosition = $('#'+hiddenInputId)[0].value;
    if (curPosition > 0)
        $(window).scroll(curPosition);
}

// determine the scroll position (cross browser code)
function scrollPosition() {
    var n_result = window.pageYOffset ? 
                   window.pageYOffset : 0;
    var n_docel = document.documentElement ? 
                  document.documentElement.scrollTop : 0;
    var n_body = document.body ? 
                 document.body.scrollTop : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

// on load of the page, load the previous scroll position
$(document).ready(function(){loadScrollPosition();});
// on scroll of the page, update the input field
$(window).scroll(function(){storeScrollPosition();});

. , :)

+3

Source: https://habr.com/ru/post/1761922/


All Articles