Some time ago, I always encountered questions in the forums including this thread and this thread on how execute codes when a user closes the browser.
AFAIK, the only events that you can use when the browser is closed is the onunload or the onbeforeunload event.
The following code blocks below will simulates the closed button of the browser when the user invoked the button and display an alert message informing the user that the window is about to close..
Using the onunload event in the body element:
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function doSomething()
{
alert('The Widnow is about to close');
}
</script>
</head>
<body onunload="doSomething();">
<form id="form2" runat="server">
</form>
</body
</html>
|
Using the onbeforeunload event:
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
window.onbeforeunload = doSomething;
function doSomething()
{
return "The window is about to close, Continue?";
}
</script>
</head>
<body>
<form id="form2" runat="server">
</form>
</body>
|
That's it! Hope you will find this example useful!