在Chrome上显示类似于firefox的警告消息在窗口关闭。确认消息“离开”或“留在此页面”不是必需的

时间:2013-12-09 07:38:43

标签: javascript jquery

我想在网页窗口关闭之前显示一条警告消息。以下代码适用于firefox和IE,但它没有在chrome和safari上显示警告消息。

**window.onbeforeunload=uEvent;
function uEvent()
{
  alert("you are being logged out");
}**

对于chrome,我尝试过使用 返回“你正在退出”;代替提示消息,但此警告消息为用户提供“离开此页面”或“留在此页面”的选项,我不希望这样做。 请帮帮我,以便chrome和safari也有类似firefox和IE

的行为

1 个答案:

答案 0 :(得分:0)

感谢Youri Arkesteijn,您可以使用以下代码:

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;  

你可以在stackoverflow上看到这个question ... 我在chromeI.EMozila ...

中进行了检查