Thomas Lee Thomas Lee - 8 months ago 53
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;


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