This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes which event will fires before closing the browser window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "which event will fires before closing the browser window" Watch "which event will fires before closing the browser window" New topic
Author

which event will fires before closing the browser window

Venkata Ramana
Greenhorn

Joined: Feb 14, 2002
Posts: 17
Hi
When i close the browser window using the
'X' in the top right hand side of the browser window, i want to execute a method of javaScript.
Which event i need to use.
Ramana
Eric Peterson
Ranch Hand

Joined: Jan 08, 2002
Posts: 31
I believe the javascript event you are looking for is onClose();


Eric Peterson
Charlie Sturman
Ranch Hand

Joined: Apr 04, 2002
Posts: 112
Originally posted by Ramana:
Hi
When i close the browser window using the
'X' in the top right hand side of the browser window, i want to execute a method of javaScript.
Which event i need to use.
Ramana

javascript:
window.onunload = function(){ alert(); }
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Use can use onload attribute in the body tag
to specify the method to be executed.
But remember the event onunload gets fired when the refresh/reload button of the browser get clicked.


Groovy
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

The event is onunload and not unload as written
by me in my previous post.

Originally posted by pradeep bhat:
Use can use onload attribute in the body tag
to specify the method to be executed.
But remember the event onunload gets fired when the refresh/reload button of the browser get clicked.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

Ramana,
I hate to say this but, your name does not comply with the Javaranch naming guidelines which can be found at http://www.javaranch.com/name.jsp
please register again with a valid name.


SCJP
Visit my download page
Dave Appleton
Greenhorn

Joined: Jan 20, 2002
Posts: 2
The way to detect if a wiindow is closed is with an unload function and some extra checking of the event object
function onClose()
{
if ((window.event.clientX<0) && (window.event.clientY<0))
alert("you closed me");
}
...
<body onunload="onClose()">
Have Fun.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
PLEASE LOOK AT THE DATE
this post was posted April 14, 2002.
I really doubt a person needs the answer anymore!
Eric
[edit]Plus if I remember correctly this solution you proposed only works in IE[/edit]
[ December 21, 2003: Message edited by: Eric Pascarello ]
Brendan Caffrey
Greenhorn

Joined: Jan 07, 2004
Posts: 1
The question may have been old - but the most satisfactory answer ( in my case anyway) was that provided by Dave Appleton.
Surely adding new and correct information can't be a bad thing.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The answer is not a cross browser supported answer plus I can give that answer to anyone that asks. Plus since no one seems to know what a search button is lately I doubt they would have found the answer.
I have people come in here before and answer 15 questions that were posted way in the past, I do not want that to be a habit of new posters.
If you used the submit button, I would be impressed.
Eric
[ January 07, 2004: Message edited by: Eric Pascarello ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: which event will fires before closing the browser window