# Executing code when the user hits the X button on the browse

**URL:** <https://www.ruby-forum.com/t/executing-code-when-the-user-hits-the-x-button-on-the-browse/115107>\
**Category:** Rails\
**Created:** [September 20, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/executing-code-when-the-user-hits-the-x-button-on-the-browse/115107 "2007-09-20T16:59:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [September 20, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/executing-code-when-the-user-hits-the-x-button-on-the-browse/115107/1 "2007-09-20T16:59:47Z")

</div>

I am writting an app that is going to record login times, logout times  
and the session data of a user of the system. This will work if the  
user manually logs out, but I would like it to record when the user  
quits if the users exits the browser (or goes to a page not in our  
application). Any ideas on how to accomplish this task?

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [September 20, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/executing-code-when-the-user-hits-the-x-button-on-the-browse/115107/2 "2007-09-20T18:48:55Z")

</div>

Well, there’s no good way to do that easily or perfectly. But here are  
some ideas.

You could try to do fancy javascript to ‘catch’ the closing of the  
window. In my experience, that’s not really that reliable though, so I  
forget how you do that, because I generally don’t try.

If you store your sessions in the database and make sure there’s an  
updated\_at column, then the updated\_at column will the last time the  
user browsers sent a request to your app. So if they didn’t manually log  
out, you could look at that updated\_at field. You could have a cron-job  
which periodically went through and says "if the user didn’t manually  
log out, but their session hasn’t been updated in an hour (or whatever),  
we’ll set their log out time to equal the session updated\_at.

But let’s say a user is staring at the same screen for an hour—the  
session updated\_at might be an hour old, but they’re still staring at  
the screen. Well, you could use periodically\_call\_remote to have an AJAX  
function called every five seconds or whatever. As long as they are  
looking at that page, the AJAX function will be called every five  
seconds. If they stop, it will stop being called. So what does the  
function do? It doens’t have to do _anything_, as long as it contacts  
the server, session.updated\_at will be updated, and then see the above  
paragraph again.

Hope this gives you some ideas.

Jonathan

White W. wrote:

> I am writting an app that is going to record login times, logout times  
> and the session data of a user of the system. This will work if the  
> user manually logs out, but I would like it to record when the user  
> quits if the users exits the browser (or goes to a page not in our  
> application). Any ideas on how to accomplish this task?
> 
> Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [September 20, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/executing-code-when-the-user-hits-the-x-button-on-the-browse/115107/3 "2007-09-20T19:25:08Z")

</div>

On 20 Sep 2007, at 16:58, White W. wrote:

> I am writting an app that is going to record login times, logout times  
> and the session data of a user of the system. This will work if the  
> user manually logs out, but I would like it to record when the user  
> quits if the users exits the browser (or goes to a page not in our  
> application). Any ideas on how to accomplish this task?

There’s not out-of-the-box way (javascript doesn’t work, at least not  
for now), but you could do it with Juggernaut if it’s really  
important to you: [http://ncavig.com/blog/?p=30#more-30](http://ncavig.com/blog/?p=30#more-30)  
Haven’t tried it out, but it seems very nice.

Best regards

Peter De Berdt
