# Detecting the close of a browser

**URL:** <https://www.ruby-forum.com/t/detecting-the-close-of-a-browser/97846>\
**Category:** Rails\
**Created:** [July 10, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/detecting-the-close-of-a-browser/97846 "2006-07-10T22:30:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![randomuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@randomuser](https://www.ruby-forum.com/u/randomuser)\
**Post date:** [July 10, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/detecting-the-close-of-a-browser/97846/1 "2006-07-10T22:30:12Z")

</div>

Hi,

I noticed that [www.meebo.com](http://www.meebo.com) has the ability that when you close the  
browser during a session, it prompts you , are you sure you want to  
close?

How does it do this? I would like to use such an ability , so that if  
the user preempts a close on the browser, I can kill all threads I have  
running for that user.

Thanks

---

<div class="post-metadata">

**Author:** ![randomuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@randomuser](https://www.ruby-forum.com/u/randomuser)\
**Post date:** [July 10, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/detecting-the-close-of-a-browser/97846/2 "2006-07-10T22:43:38Z")

</div>

On 7/10/06, Randomuser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I noticed that [www.meebo.com](http://www.meebo.com) has the ability that when you close the  
> browser during a session, it prompts you , are you sure you want to  
> close?
> 
> How does it do this? I would like to use such an ability , so that if  
> the user preempts a close on the browser, I can kill all threads I have  
> running for that user.

That’s not really Rails-specific, but you may wish to look for info on  
JavaScript/onclose:

[http://www.google.com/search?hl=en&q=javascript+onclose&btnG=Google+Search](http://www.google.com/search?hl=en&q=javascript+onclose&btnG=Google+Search)

There may be other ways to do it, but ‘onclose’ is one.

Thanks

I hope that helps.

Sincerely,  
James Anderson.

---

<div class="post-metadata">

**Author:** ![randomuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@randomuser](https://www.ruby-forum.com/u/randomuser)\
**Post date:** [July 10, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/detecting-the-close-of-a-browser/97846/3 "2006-07-10T22:47:03Z")

</div>

Randomuser wrote on 10.07.2006 22:30:

> Hi,
> 
> I noticed that [www.meebo.com](http://www.meebo.com) has the ability that when you close the  
> browser during a session, it prompts you , are you sure you want to  
> close?
> 
> How does it do this? I would like to use such an ability , so that if  
> the user preempts a close on the browser, I can kill all threads I have  
> running for that user.

onUnload JavaScript

---

<div class="post-metadata">

**Author:** ![randomuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@randomuser](https://www.ruby-forum.com/u/randomuser)\
**Post date:** [July 11, 2006, 7:04am UTC](https://www.ruby-forum.com/t/detecting-the-close-of-a-browser/97846/4 "2006-07-11T07:04:21Z")

</div>

Using the Javascript onunload definitely isn’t 100% perfect. It’s got so  
many browser compatibility issues. After trying to use this piece of  
javascript, we ended up with other techniques to end sessions aside from  
onUnload to act as a fail safe measure.

For a time, when we were contemplating on how meebo was using onUnload,  
we were even thinking if that confirmation box/alertbox honestly had any  
logic behind it (was it really closing sessions) because it sure was  
difficult to work with.

Just some thoughts.

Thanks,

Bing

---

<div class="post-metadata">

**Author:** ![randomuser](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@randomuser](https://www.ruby-forum.com/u/randomuser)\
**Post date:** [July 10, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/detecting-the-close-of-a-browser/97846/5 "2006-07-10T23:37:48Z")

</div>

- Randomuser (removed\_email\_address@domain.invalid) [060710 15:33]:

> How does it do this? I would like to use such an ability , so that if  
> the user preempts a close on the browser, I can kill all threads I have  
> running for that user.

(See other post in this thread for a JavaScript means of alerting on  
.close())

If you mean you wish to clean up server-side threads, then be sure to  
have some other failsafe means of performing the task – if the client  
is not JavaScript enabled, has JavaScript turned off, or simply drops  
off the Internet (or in dozens of other scenarios) then no notification  
will be coming from the user to tell the server to clean up their  
threads.

FWIW, I’d probably greasemonkey to /dev/null anything that behaved even  
remotely like that example site’s behavior. $0.02. 🙂

## Rick

[http://www.rickbradley.com](http://www.rickbradley.com) MUPRN: 670  
| than would be the case  
random email haiku | if they renewed with Go  
| Daddy Software.
