How to run a javascript if the web page window is closed

Hi guys,

I want to run a javascript function when the browser window is closed.i’m developing jsp application.i want execute a update database when the user closes the window.i think i can do this by putting update query in a javascript function and executing this query when the window is closed.

If any of you have any other apporach then pls let me know.

thank you.

May you pls tell me how to use the ajax code for doing that.may you pls pass me links that has code for doing this.

Thank you once again.

Have you asked Google? Google knows a lot. I doubt you’ll find a copy-and-paste solution, because your code needs to talk to your server. At any rate, if you Google it, you should be able to find what you need. Google is your friend.

Since JavaScript runs in the web browser window it must run before the window is closed. Once the window closes the JavaScript is gone.

The only way to have the server detect when the person leaves the web page (whether they close the browser window or simply open a page from somewhere else is to use an ajax script that sends an “I.m still open” message to the server at regular intervals. Once a long enough time has passed since the last such signal was received then the server knows that the JavaScript is no longer running - in most cases because the person left the web page.