简体   繁体   中英

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:

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:(

I tried different combinations: added event.returnValue = '':

window.addEventListener("beforeunload", function(event) {
    unloading_page_func();
    event.returnValue = '';
}, false);
  • Chrome & Edge generate confirmation prompts, Firefox still does nothing.

Added event.returnValue = null:

window.addEventListener("beforeunload", function(event) {
    unloading_page_func();
    event.returnValue = null;
}, false); 
  • All 3 browsers, Chrome & Edge & Firefox, generate confirmation prompts.

Added event.preventDefault():

window.addEventListener("beforeunload", function(event) {
    unloading_page_func();
    event.preventDefault();
}, false); 
  • Chrome & Edge work quietly, but Firefox generates confirmation prompts.

Tried different combinations of both, event.preventDefault() & event.returnValue - always one of the results listed above. 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?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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