The moose likes HTML, CSS and JavaScript and the fly likes javascript to detect browser closing 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 "javascript to detect browser closing" Watch "javascript to detect browser closing" New topic

javascript to detect browser closing

c ccc

Joined: Dec 23, 2005
Posts: 2

i need to have a way to detect the browser closing event, so I can invoke my servlet to update the database ( time the user spent for the application). i tried to use onunload to trigger the servlet, but, it actually triggers for the back button clicking, reload, and any link clickings as well. Is there a way, javascript can detect the close button clicking only?

thanks a lot.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

c ccc,

welcome to the Javaranch. You may not have noticed our naming policy on the way in. it requires a real sounding first and last name, although a first name initial is ok. please go here and update your screen name.

edit - i just noticed you have been asked before to change your name.

Second, i'm not sure this is the best forum for this question. I'm going to see about getting it moved to a more appropriate venue, where hopefully you can get the help you need.
[ January 12, 2006: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Let's move this beaut to s ssservlets.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Actually, it'd be more appropriate in the HTML forum, where this very subject has been broached many a time.

Moving forward (or is it a redirect? ) ...

[Asking smart questions] [About Bear] [Books by Bear]
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
There is no real way to detect it.

Rely on your session end event on the server.

I agree. Here's the link:
subject: javascript to detect browser closing
It's not a secret anymore!