/    Sign up×
Community /Pin to ProfileBookmark

Detect Tab Close

Ehh…. event handlers go in the ‘HTML’ section, right(they are coded in HTML, although can be done in JS, and deal with JS… ugh)?

Well, reguardless my problem is simple. I have a simple AJAX chatroom. You can log on simply by entering a name. The system is very simple, but checks names to make sure no two people have the same one, and there are a few(very few) other small features.

But of course most people who ever use it don’t seem to notice or use that ‘Log Out’ button I made. More times than not they just want to close the tab(or window if they are using a non-tabbed browser([I]if those still exist…[/I])). The problem with this is my system isn’t designed to log out users from a server side stand point. I have an automatic 15 minute log out for inactive users(if you leave the tab/window open), but nothing server side.

I tried to use ‘onunload’ for the <body> tag, but this will only fire that logout function if you refresh the tab/window, or type a new address and proceed to go there. If the actual tab/window is simply closed, the function either doesn’t run, or can’t trigger fast enough.

Could it be possible that I need the onunload event on something else rather than the <body>? Maybe the document element instead? but of course how do I set a document event?

to post a comment
HTML

5 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzNov 14.2007 — Some events are HTML attributes, but their values are JS, so this is a JS question.

Have you tried using the onbeforeunload event?

It's best to avoid using inline event handlers whenever possible. I suggest you read up on [url=http://www.dynamicsitesolutions.com/javascript/best-practices/#unobtrusive_javascript]Unobtrusive JavaScript and other JavaScript Best Practices[/url].
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorNov 15.2007 — While I understand the basis for the article in the link, what else could be suggested for my situation? If the user closes the tab or window, I have to log them out or they will never be logged out(unless I manually go in and do it myself). So I need to run the logout script when the tab or window is closed, and as far as I know, this is simply a matter for an event, since we need to do something when an event occurs...

Just wondering for input because this is the only way I could come up with.


Also, on a much sadder note, it appears my server froze... I blame it on myself. There must have been something in my code because when I changed the event to 'onbeforeunload' and closed a tab, my site essentially stopped. It doesn't load now(probably because of some loop, i would guess in PHP, although there are not any for() loops in my code....) Ehhh.... it sucks, but maybe I can get the admin to reset everything for me....
Copy linkTweet thisAlerts:
@KravvitzNov 15.2007 — For this you can do something like this:
&lt;script type="text/javascript"&gt;/* &lt;![CDATA[ */
window.onbeforeunload = function(){
doSomething('someArgument');
}
/* ]]&gt; */&lt;/script&gt;

For other kinds of events, I recommend [url=http://dean.edwards.name/weblog/2005/10/add-event2/]Dean Edwards' event manipulation functions[/url].
Copy linkTweet thisAlerts:
@Sup3rkirbyauthorNov 15.2007 — oh, ok so you just mean set all the events with javascript, rather than code them into the HTML, right?

And I believe your event works for me. Talked to some support and we got some silly server issue resolved.
Copy linkTweet thisAlerts:
@KravvitzNov 15.2007 — Yes, that's what I meant.
×

Success!

Help @Sup3rkirby spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...