How to avoid the “Are you sure you want to navigate away from this page?” when changes committed? on pagination click of gridview in IE

不羁岁月 提交于 2019-12-24 07:37:11

问题


My web page contains grid-view with many input fields.I want to give alert to user before moving to next tab. My script is working fine in Mozilla Firefox and chrome but in IE the alert is also coming for grid-view pagination click.I don't want to show on pagination click.but in Mozilla and chrome this doesn't happen. How to avoid this .

Here I am giving my script

 <script>

    var warnMessage = "You have unsaved changes on this page!";

    $(document).ready(function () {
        $('input:not(:button,:submit),textarea,select').change(function () {
            window.onbeforeunload = function () {
                if (warnMessage != null) return warnMessage;
            }
        });
        $('input:submit').click(function (e) {
            warnMessage = null;
        });
    });



</script>

How to avoid of this message in gridview pagination click in IE only? Can anybody help me out?

Thanks


回答1:


just set the the value to null on pagination click

   window.onbeforeunload = null;

UPDATE 1

try this one instead of your code:

  var myEvent = window.attachEvent || window.addEventListener;
    var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
    myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
        var confirmationMessage = 'Are you sure to leave the page?';  // a space
        (e || window.event).returnValue = confirmationMessage;
        return confirmationMessage;
    });

UPDATE 2

after you use window.onbeforeunload = null; in your pagination control to rebind the alert in your update panel try this solution, it may help you with that issue:

   $(document).ready(function () {
     bindPageAlert();
   });

    function bindPageAlert()
    {
        var myEvent = window.attachEvent || window.addEventListener;
        var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';
        myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
            var confirmationMessage = 'Are you sure to leave the page?';  // a space
            (e || window.event).returnValue = confirmationMessage;
            return confirmationMessage;
        });
    }

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(bindPageAlert);


来源:https://stackoverflow.com/questions/24117366/how-to-avoid-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!