Thomas Lee Thomas Lee - 1 year ago 140
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
, and
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 Source

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() {});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download