File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes onUnload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "onUnload" Watch "onUnload" New topic


Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
hi all,

Please have a look at my html

if i click on the 'submit' button im getting the alert message "submit me" but if i close the window im not getting "submit me" message but im getting "gonna submit form".. why is that ?

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Clicking your submit button will submit the form, meaning it will submit it to whatever is in the action attribute of your form tag. So your action attribute calls the JavaScript the pops up the alert box with 'submit me' in it.

When you close the window, the onUnload attribute of the body tag gets called, and your onUnload attribute calls the JavaScript funtion callme() which pops up an alert box with the text "gonna submit form" in it.
Rajeev Ravindran
Ranch Hand

Joined: Aug 27, 2002
Posts: 455
hey Stephen , thanks for the reply !!

Just to figure out watz happening there, i invoked a servlet on form submit and i found its too inconsistent. Sometimes when i close the browser window tat servelt is getting invoked sometimes its not !!

What i really need is , i want to a servlet to be executed whenever i close the window but when i dont want the servlet to execute if i refresh the window.. any help on this is highly appretiated.. i have spend a lot of time on this.. i dont trust calling js funtion onUnload() and checking clientX and clientY position, it sometimes works properly on IE + Win XP but the same wasnt working on IE + Win 2000.. any tips or tricks ?

Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
There is no real way to determine if the window is closing, going to another page, or being refreshed.

I agree. Here's the link:
subject: onUnload
It's not a secret anymore!