【问题标题】:Show alert message on chrome similar to firefox on window close.Confirmation message "Leave" or "Stay on this page" not required在关闭窗口时在 chrome 上显示类似于 firefox 的警报消息。不需要确认消息“离开”或“留在此页面上”
【发布时间】:2013-12-09 07:38:43
【问题描述】:

我想在网页关闭窗口之前显示一条警报消息。下面的代码在 Firefox 和 IE 上运行良好,但它没有在 chrome 和 safari 上显示警报消息。

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

对于 chrome,我尝试使用 返回“您正在注销”;代替警报消息,但此警报消息为用户提供了“离开此页面”或“留在此页面上”的选项,这在我的情况下是不想要的。 请帮助我,以便 chrome 和 safari 也有类似 firefox 和 IE 的行为

【问题讨论】:

  • 那是浏览器控制的。不确定您是否可以在 Chrome 中修改该行为。

标签: javascript jquery


【解决方案1】:

感谢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 中检查了这个...

【讨论】:

  • 我已经尝试过这个解决方案,但它显示的不是警告消息而是确认消息,即“离开此页面”或“留在此页面上”,我不希望用户确认...应该单击用户注销后,警报消息上只有“确定”按钮
  • 是的...Kiyarash...完全符合我标准的不同代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多