简体   繁体   中英

How to capture the browser window close event on PHP?

I found the code below in stackoverflow and it works fine, but how can I capture <input type="button"> ?

I tried $('input').on('click') , $('input.button').on('click') , and $('button').on('click') also didn't work. Also is there any method to capture refresh event? Many thanks.

var inFormOrLink;
$('a').on('click', function() { inFormOrLink = true; });
$('form').on('submit', function() { inFormOrLink = true; });

$(window).on("beforeunload", function() { 
    return inFormOrLink ? "Do you really want to close?" : null; 
})

To capture click on the <input type="button"> you can do

$('input[type=button]').on('click', function() {});

To capture refresh event you can do

window.onbeforeunload = function(e) {};

To capture refresh event with jQuery you can use unload

$(window).unload(function() {});

If I get you correctly, you want to know when a tab/window is effectively closed. Well, AFAIK the only way in Javascript to detect that kind of stuffs are onunload & onbeforeunload events.

Unfortunately (or fortunately?), those events are also fired when you leave a site over a link or your browsers back button. So this is the best answer I can give, I don't think you can natively detect a pure close in Javascript. Correct me if I'm wrong here.

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