在chrome上显示类似于关闭窗口时的firefox的警报消息.确认消息”;离开"或“;留在这个页面上&quo

Show alert message on chrome similar to firefox on window close.Confirmation message "Leave" or "Stay on this page" not required

本文关键字:消息 quot chrome quo 离开 firefox 类似于 窗口 确认 显示      更新时间:2024-05-03

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

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

对于铬,我尝试使用return"您正在被注销";代替警报消息,但此警报消息为用户提供了"离开此页面"或"留在此页面"的选项,这在我的情况下是不希望的请帮我一下,这样chrome和safari也有类似的行为,比如firefox和IE

感谢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
我在chromeI.EMozila中检查了这个。。。