如何在mozilla firefox中关闭浏览器时提醒用户 [英] How to alert user while closing the browser in mozilla firefox

查看:74
本文介绍了如何在mozilla firefox中关闭浏览器时提醒用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在Mozilla Firefox中关闭浏览器时建议一种提醒用户的方法。我已经尝试过针对IE和Chrome的onbeforeunload,它在两者中都有效。但我无法在Firefox中获得相同的响应。似乎onbeforeunload和onunload在Firefox中不起作用。此外,在一些博客中,我发现onbeforeunload仅适用于Firefox 4.0或更低版本。我尝试了下面的javascript代码,它适用于我和IE浏览器,但不适用于Firefox。





感谢,如果有其他选择可用。



谢谢,

Nishant



什么我试过了:



Please suggest a way to alert user while closing the browser in Mozilla firefox. i have tried the onbeforeunload for IE and Chrome and it does work in both. But I am unable to get the same response in Firefox. It seems that onbeforeunload and onunload is not working in Firefox. Also, in some of the blogs I have found that the onbeforeunload only works with Firefox 4.0 or less version. I have tried the below javascript code and it works for me in IE and Chrome but not in Firefox.


Appreciate, if any alternative is available.

Thanks,
Nishant

What I have tried:

<script language="JavaScript" type="text/javascript">
        var myEvent = window.attachEvent || window.addEventListener;
        var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; 

        myEvent(chkevent, function (e) {
            var confirmationMessage = 'Are you sure to leave the page?';
            (e || window.event).returnValue = confirmationMessage;
            return confirmationMessage;
        });

</script>

推荐答案

onbeforeunload 事件 [ ^ ]适用于所有桌面浏览器,包括最新版本的Firefox。



但是,Chrome (自v51起),Firefox (自v44起)以及Safari (因为v9.1)将使用标准消息替换您的自定义消息安全原因。



唯一的解决方法是尝试挂钩任何导致用户离开页面的事件,并在浏览器处理事件之前显示消息。这可能变得非常复杂,因为您需要处理表单提交,点击< a> 元素(但不是那些在新窗口中打开或触发的元素页内行动)
The onbeforeunload event[^] works in all desktop browsers, including the latest version of Firefox.

However, both Chrome (since v51), Firefox (since v44), and Safari (since v9.1) will replace your custom message with a standard message for "security reasons".

The only workaround is to try to hook any events that will result in the user leaving the page, and display your message before the browser handles the event. This can get very complicated, as you need to handle form submission, clicks on <a> elements (but not those that open in a new window, or trigger an in-page action), etc.


这篇关于如何在mozilla firefox中关闭浏览器时提醒用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆