关闭对话框子页面时,保持页面在父页面上滚动

时间:2019-05-02 07:33:11

标签: javascript c# jquery html dialog

我当前正在使用一个新对话框,并通过页面中的链接打开该对话框,一旦关闭该对话框,它会刷新父页面,但我的问题是如何保持父页面当前滚动刷新

这是对话框的关闭代码:


$("#dialogStatus").dialog({
            autoOpen: false,
            autoResize: true,
            resizable: false,
            width: 450,
            height: 400,
            modal: true,
            position: ['center', 40],
            close: function (event, ui) {
                var sss = $("#Id").val();
                var hNotif = $("#hiddenNotification").val();
                var ddlvalue = $('#NewFilterBy option:selected').val();
                var ddlStat = $('#StatusId option:selected').val();

                var url = window.location.href;
                var newUrl = updateQueryStringParameter(url, "Id", sss, "ddlFilter", ddlvalue, "statusFilter", ddlStat);
                window.location = newUrl;
            }
        });

现在发生的事情是当我关闭它时,它会刷新,并且父级停留在滚动位置停留在页面顶部

编辑:尝试添加document.location.reload(),但添加后会丢失参数

1 个答案:

答案 0 :(得分:0)

您可以尝试在URL字符串中放入参数'scrollPosition',然后在加载文档时向下滚动到该位置。

$(document).ready(function(){

   //The function that scroll down
   var positionToScroll ; //You set here your variable from your server variable scrollPosition
   $(window).scrollTop(positionToScrol);


   $("#dialogStatus").dialog({
         autoOpen: false,
         autoResize: true,
         resizable: false,
         width: 450,
         height: 400,
         modal: true,
         position: ['center', 40],
         close: function (event, ui) {
             var sss = $("#Id").val();
             var hNotif = $("#hiddenNotification").val();
             var ddlvalue = $('#NewFilterBy option:selected').val();
             var ddlStat = $('#StatusId option:selected').val();

             var url = window.location.href;
             var currentScrollPosition = $(window).scrollTop(); // The position of  the scroll bar 
             var newUrl = updateQueryStringParameter(url, "Id", sss, "ddlFilter", ddlvalue, "statusFilter", ddlStat,"scrollPosition",currentScrollPosition );
             window.location = newUrl;
         }
      });


});

编辑:如果无法从服务器上设置JS变量“ positionToScroll”,则可以使用此

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true :  decodeURIComponent(sParameterName[1]);
        }
     }
};


var positionToScroll  = getUrlParameter('scrollPosition');