简体   繁体   中英

Fire a function only on browser close

The idea is to show a customized popup / div when user closes the browser. Found this code online and it works for me partially.

JS:

window.onbeforeunload = function (e) {            
            e = e || window.event;
            if (e) {
                e.returnValue = '';
                $("#popup").show();
            }            
        }

HTML:

<div id="popup" class="popup">
</div>

When user closes the browser, an alert pops up with a confirmation, to leave or stay. Is it possible to show the popup/div without showing the default JS alert.

This piece of code also fires when user refreshes the page. How to allow this only on close event.

Only works in the more recent browsers to ignore the alert with a return if you don't use jquery:

window.onbeforeunload = function (e) {            
     e = e || window.event;
     if (e) {
         $("#popup").show();
     }

    return null;        

}

Events (e), actually never should be edited

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