Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:无法在页面卸载之前显示警报_Javascript_Jquery_Asp.net - Fatal编程技术网

Javascript:无法在页面卸载之前显示警报

Javascript:无法在页面卸载之前显示警报,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,以下是我为请求用户在卸载页面上确认而编写的代码: $(document).ready(function () { var pageLeaveEvent = window.attachEvent || window.addEventListener; var checkedEvent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; //Make IE7, IE8 compatible.

以下是我为请求用户在卸载页面上确认而编写的代码:

    $(document).ready(function () {
        var pageLeaveEvent = window.attachEvent || window.addEventListener;
        var checkedEvent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; //Make IE7, IE8 compatible.

        pageLeaveEvent(checkedEvent, function (e) { //For >= IE7, Chrome & Firefox.
            debugger;
            return 'Are you sure you want to leave page?';
        });
    });
刷新页面时,会点击
pageLeaveEvent
中的调试器,但不会显示警报。我把头撞在墙上想解决这个问题,却不知道为什么会这样。请帮我找出原因


顺便说一句,以前我在ASP.NETMVC项目中使用了相同的代码,现在我正在尝试在ASP.NETWebForms项目中使用相同的代码。我找到了此代码。

您错过了您链接的教程中的event returnValue配置:

(e || window.event).returnValue = 'your_message';

将它添加到回调中,它将正常工作。

如果删除
调试器
?…您需要设置事件的retrunValue属性:
var msg='您确定要离开页面吗?';(e | | window.event).returnValue=msg;返回味精&&