简体   繁体   English

无法让 Chrome、Edge 和 Firefox 安静地执行 beforeunload 事件

[英]Can't make Chrome, Edge & Firefox to quietly execute beforeunload event

I want to do something when the user clicks on some other link ref on the current page and going to leave this current page.当用户单击当前页面上的其他链接引用并离开当前页面时,我想做一些事情。 I do NOT want any confirmation prompt - just silent execution of my function. According to the specs and numerous responses on Google in order to NOT have this confirmation prompt, I simply shall NOT have the event preventDefault() or event.returnValue inside my event listener:我不想要任何确认提示 - 只是静默执行我的 function。根据规范和谷歌上的大量回复,为了没有这个确认提示,我根本不会在我的事件中有事件 preventDefault() 或 event.returnValue听众:

window.addEventListener("beforeunload", function(event) {
    unloading_page_func();
}, false);

Such code works nice in Chrome and Edge, but Mozilla does nothing - not sure whether it is NOT adding the eventlistener or not triggering the beforeunload event: neither console.log nor alert work during beforeunload:(这样的代码在 Chrome 和 Edge 中运行良好,但 Mozilla 什么都不做——不确定它是否没有添加事件监听器或没有触发 beforeunload 事件:console.log 和 alert 在 beforeunload 期间都不起作用:(

I tried different combinations: added event.returnValue = '':我尝试了不同的组合:添加了 event.returnValue = '':

window.addEventListener("beforeunload", function(event) {
    unloading_page_func();
    event.returnValue = '';
}, false);
  • Chrome & Edge generate confirmation prompts, Firefox still does nothing. Chrome & Edge 生成确认提示,Firefox 仍然没有任何反应。

Added event.returnValue = null:添加了 event.returnValue = null:

window.addEventListener("beforeunload", function(event) {
    unloading_page_func();
    event.returnValue = null;
}, false); 
  • All 3 browsers, Chrome & Edge & Firefox, generate confirmation prompts.所有 3 种浏览器,Chrome & Edge & Firefox,都会生成确认提示。

Added event.preventDefault():添加了 event.preventDefault():

window.addEventListener("beforeunload", function(event) {
    unloading_page_func();
    event.preventDefault();
}, false); 
  • Chrome & Edge work quietly, but Firefox generates confirmation prompts. Chrome 和 Edge 运行安静,但 Firefox 会生成确认提示。

Tried different combinations of both, event.preventDefault() & event.returnValue - always one of the results listed above.尝试了两者的不同组合,event.preventDefault() 和 event.returnValue - 始终是上面列出的结果之一。 Any recommendations on how to make my Chrome (ver 92.0.4515.131), Edge (ver 92.0.902.67), and Firefox (ver 90.0.2) quietly execute my function without generating any confirmation prompt?关于如何让我的 Chrome(版本 92.0.4515.131)、Edge(版本 92.0.902.67)和 Firefox(版本 90.0.2)安静地执行我的 function 而不生成任何确认提示的任何建议?

As 'Bravo' commented the behavior I am experiencing may be related to the content of the unloading_page_func() I want to ring when the user switching to a different page - this function removes this eventlistener to the 'beforeunload', clears interval of the function that updates the page, and sends a GET request to the server where some cleanup is done:正如“Bravo”评论的那样,我遇到的行为可能与 unloading_page_func() 的内容有关 我想在用户切换到另一个页面时响铃 - 这个 function 将这个事件监听器删除到“beforeunload”,清除 function 的间隔更新页面,并向完成某些清理的服务器发送 GET 请求:

function unloading_page_func()
{
    try {
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        try {
            xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e) {
                alert("This browser does not support AJAX!");
            }
        }
    }

    window.removeEventListener("beforeunload", unloading_page_func, false);

    clearInterval(update_display_interval_id);

    xmlHttp.open('GET', '/unload_page', true);
    xmlHttp.send();
};

Using 'beacon' as Bravo recommended solved my problem.按照 Bravo 的建议使用“信标”解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM