Thomas Lee Thomas Lee - 4 months ago 33
Javascript Question

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;
})

Answer

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() {});
Comments