Forum Moderators: open

Message Too Old, No Replies

Window Close Event in Java Script

Trape IE window close Event

         

prabhunkl

9:27 am on Mar 1, 2002 (gmt 0)

10+ Year Member



Hi,

I like to give a alert to the user while closing the IE & Netscape window, with the OK and Cancel option. Is it possible with JavaScript? any one can Help me regarding this.

tedster

11:36 am on Mar 1, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to the Forums!

You cannot directly capture the window Close event. You can only capture onUnload and set a variable "flag" on every link to be sure that the onUnload isn't triggered because of a link being followed.

Here's a reference that offers some sample code:
[developer.irt.org...]

joshie76

1:07 pm on Mar 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just thought I'd add this for reference in case somebody else comes a looking in here.

IE has (yet another) non-standard but nontheless useful event called onBeforeUnload. Using the event.returnValue property you can present the visitor with a warning if they try to navigate away from your page. eg:


<script>
function handleUnload()
{
event.returnValue = "Click cancel to return to the blah";
}
</script>

<body onbeforeUnload="handleUnload()">

If the user clicks Cancel then their navigation action is cancelled and the page doesn't unload.

You don't have much control over the dialog that appears (for obvious security reasons) but this event can be very useful in application territory to catch unsaved changes and the like. I've also noted that a few of the on-line banks are using this event in a frameset to warn people that they should logout etc.

Josh