12

我有一个系统,我想与用户核实他们是否确定一旦设置了脏标志就想离开页面。

我正在使用以下代码 - 在 FireFox 中,我可以通过 FireBug 查看页面源,并且标签中正确插入了 onbeforeunload 属性。

在 Chrome 和 FireFox 中,这不会发生,我可以在没有任何警告的情况下离开页面。更新 body 标记的 jQuery 行肯定正在执行,只是没有执行。

if ($("body").attr('onbeforeunload') == null) {
    if (window.event) {
        // IE and Chrome use this
        $("body").attr('onbeforeunload', 'CatchLeavePage(event)');
    }
    else {
        // Firefox uses this
        $("body").attr('onbeforeunload', 'return false;CatchLeavePage(event)');
    }
}

任何想法如何从这里开始?

4

5 回答 5

17

您不能通过返回 false 来中止页面卸载。您必须返回将在消息框中显示给用户的字符串,并且他决定是否要离开或留在页面上(通过选择“确定”或“取消”按钮),因此您需要编写代码这:

 window.onbeforeunload = function() {
    return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse...
 };
于 2009-11-26T10:54:07.727 回答
8

试试这个

  <script type=\"text/javascript\">
        var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
        var leave_message = 'You sure you want to leave?'
        function goodbye(e) 
        {
            if(dont_confirm_leave!==1)
            {
                if(!e) e = window.event;
                //e.cancelBubble is supported by IE - this will kill the bubbling process.
                e.cancelBubble = true;
                e.returnValue = leave_message;
                //e.stopPropagation works in Firefox.
                if (e.stopPropagation) 
                {
                    e.stopPropagation();
                    e.preventDefault();
                }

                //return works for Chrome and Safari
                return leave_message;
            }
        }   
    window.onbeforeunload=goodbye;
    </script>
于 2010-10-28T23:12:25.113 回答
3
window.onbeforeunload = function () { return 'Are you sure?' };
于 2009-11-26T10:55:18.410 回答
1

检查此代码:

var validNavigation = false;

function wireUpEvents() {
var dont_confirm_leave = 0; 
var leave_message = "You sure you want to leave ?";

function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave !== 1) {
if (!e) e = window.event;
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}
}

window.onbeforeunload = goodbye;

document.onkeydown = function () {
switch (event.keyCode || e.which) {
case 116 : //F5 button
validNavigation = true;
case 114 : //F5 button
validNavigation = true;
case 82 : //R button
if (event.ctrlKey) {
validNavigation = true;
}
case 13 : //Press enter
validNavigation = true;
}

}
// Attach the event click for all links in the page
$("a").bind("click", function () {
validNavigation = true;
});

// Attach the event submit for all forms in the page
$("form").bind("submit", function () {
validNavigation = true;
});

// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function () {
validNavigation = true;
});
}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function () {
wireUpEvents();
});
于 2014-09-15T12:03:54.880 回答
0

它不漂亮,但它成功了。

var warnclose = true;
var warn = function(e) {
    var warning = 'Your warning message.';
    if (warnclose) {
        // Disables multiple calls
        warnclose = false;

        // In case we still need warn to be called again 
        setTimeout(function(){
            warnclose = true;
        }, 500);

        return warning;
    }
};
window.onbeforeunload = warn;
于 2018-01-01T21:19:40.853 回答