使用jQuery在Chrome和IE中设置body元素的onbeforeunload

时间:2009-11-26 10:41:41

标签: javascript jquery google-chrome onbeforeunload

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

我正在使用以下代码 - 在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)');
    }
}

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

5 个答案:

答案 0 :(得分:17)

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

 window.onbeforeunload = function() {
    return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse...
 };

答案 1 :(得分: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>

答案 2 :(得分:3)

window.onbeforeunload = function () { return 'Are you sure?' };

答案 3 :(得分: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();
});

答案 4 :(得分: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;