如何在chrome上离开页面创建确认对话框?

问题描述:

我知道有很多问题,但没有什么能正确回答我。我想在用户离开页面时显示确认对话框。如果用户按下取消,他将保持在页面上,如果确定,他所做的更改将通过调用方法回滚。我这样做了:



I know there are a lot of questions regarding this but nothing is answering me right. I want to show a confirmation dialog when user leaves the page. If the user press Cancel he will stay on page and if OK the changes that he has made will be rollback-ed by calling a method. I have done like this:

var _AreyousureLeaving = "Are you sure you want to leave?";
window.onbeforeunload = function (evt) {
                var message = _AreyousureLeaving;
                  if (typeof evt == 'undefined') {
                    evt = window.event;
                }
                if (evt) {
                    confirm(_AreyousureLeaving);
                    evt.returnValue = message;
                }
                return message;  
        }





此代码在IE和Firefox中运行良好。但是此代码在Chrome中无效。声明确认(_AreyousureLeaving);没有工作。



如果有办法控制这个人群?谢谢



This code is working fine in IE and Firefox. But this code is not working in Chrome. the statements confirm(_AreyousureLeaving); is not working.

if there is any way to control this popupt? Thanks





请参考下面的代码,使用IE浏览器,Mozila Firefox和Chrome ..



Hi,

Please refer below code its wroking fine with I.E, Mozila Firefox and chrome..

<script type="text/javascript" >
        window.onbeforeunload = function () {
            return "Do you want to leave?"
        }

        // A jQuery event (I think), which is triggered after "onbeforeunload"


(窗口).unload(function() {
alert(Do Reset ..);
//我将调用我的方法
});
< / script >
(window).unload(function () { alert("Do Reset.."); //I will call my method }); </script>









谢谢

Zubair





Thanks
Zubair


您是否在全局范围内定义了'_AreyousureLeaving'?你的代码使用局部变量'message'看起来有点不可思议,它不能用于某个目的,而你正在使用不推荐使用的event.returnValue。但是,我遇到了类似的问题,发现Chrome在onunload事件中自动阻止警报并确认对话框。我从来没有通过jQuery源来查看他们是如何解决这个问题的,但是如果你使用jQuery它应该解决你的问题。像这样:


Do you have '_AreyousureLeaving' defined in a global scope? Your code looks a little wonky using local variable 'message' that doesn't serve a purpose and you're using the deprecated event.returnValue. However, I had a similar issue and discovered Chrome was blocking alert and confirm dialogs automatically in the onunload event. I never traced through the jQuery source to see how they resolved the issue, but if you use jQuery it should solve your problem. Like this: