Tag: beforeunload


Confirmation on close when user closes tab/browser in ASP.Net

In the past, in my project about web application, there is requirement from user to show confirmation on close when user closes tab/browser of the application. It is like in gmail or facebook when you compose an email, you forget or close tab/browser without send email or submit your post. An confirmation will be shown to you.

There are some ways to make this, from the simple want up to the more complex. If you using ASP.NET or C#, you can put this script in masterpage or in a page. Please put this script in javascript area in your page.

Method 1:
var warnMessage = “Are you sure want to close Application?”;
$(window).load(function () {
$(window).bind(‘beforeunload’, function () { return warnMessage; })
});

Method 2:
var warnMessage = “Are you sure want to close Application?”;
$(‘input:not(:button,:submit),textarea,select’).live(“change”, function …