View Full Version : Internet Explorer closing event -catching in JavaScript

04-25-2008, 10:56 AM
Hello All of You,
I want to know that
Is there a way of catching a form_unload a type of event for Internet Explorer, with a cancel= true type of parameter?
I mean when a form/IE is exiting without saving, it gives you a chance to ask the user to confirm " Exit without saving?"


04-25-2008, 11:17 AM
document.body.onunload = function(){event.returnValue=confirm("Really quit?");}

04-25-2008, 12:05 PM
Thnxxx a Lot mjlorbet :)

Philip M
04-25-2008, 02:00 PM
document.body.onunload = function(){event.returnValue=confirm("Really quit?");}

The snag it that regardless of the response the page still unloads. The onunload event cannot be cancelled as it has already been initiated.

Try this:-

<form name = "myForm">
<input type = "text" name = "field1" size = "20">
<input type = "submit" value = "Submit" onclick ="setFlag()">

<script type = "text/javascript">

window.onbeforeunload = confirmExit;
var postback = false;

function setFlag() {
postback = true;

function confirmExit() {
if (document.myForm.field1.value.length > 0) {
if (postback == true) {
event.cancelBubble = true;
else {return "Please don't leave this page without submittting the form. If you do leave all your data will be lost.";

Where quality is the thing sought after, the thing of supreme quality is cheap, whatever the price one has to pay for it. --William James

04-25-2008, 03:37 PM
good call