SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
-
Jul 30, 2009, 13:44 #1
- Join Date
- Jul 2008
- Posts
- 81
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Know when user accesses/leaves a page
I know it is extremely easy to know when a user accesses a page. The way I do it is run a query every time a user goes to this certain page. The thing I am having trouble with is finding out when the user leaves.
I have created a chat feature to my website and am trying to get the "users online" based off on who is there at that moment updated by ajax. How do I find out a user has left the page so they are no longer counted among the "users online"? I might be posting in the wrong language forum because this might be javascript/ajax but I'm not sure.... Thanks in advance!
-
Jul 30, 2009, 13:57 #2
Keep track of the last time the user accessed, or updated there session. After a certain amount of time has passed consider the user left.
-
Jul 30, 2009, 14:14 #3
- Join Date
- Jun 2006
- Location
- Wigan, Lancashire. UK
- Posts
- 523
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
javascript onunload()or onbeforeunload() might help, but they not well supported, and triggered by a refresh as well as leaving the page
-
Jul 30, 2009, 15:02 #4
- Join Date
- Oct 2005
- Location
- Milton Keynes, UK
- Posts
- 996
- Mentioned
- 9 Post(s)
- Tagged
- 2 Thread(s)
You could keep sending small ajax requests to the server every few seconds. But it's not very nice and a big ol' waste of bandwidth unless you *really* need to know if they've left.
-
Jul 30, 2009, 19:16 #5
- Join Date
- Sep 2005
- Location
- Sydney, NSW, Australia
- Posts
- 16,875
- Mentioned
- 25 Post(s)
- Tagged
- 1 Thread(s)
Most scripts just work based on a time period. If the script hasn't heard from them in 5 minutes or 10 minutes the script assumes they've left.
If they are using a browser that supports ajax you could set up the page to signal the server every minute and then if you have previously received a signal and haven't for over a minute then you know they've left.
Doesn't stop them leaving your page open while they go off foe an hour for lunch. Just because the page is still displayed doesn't mean that anyone is looking at it.Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">
-
Jul 30, 2009, 19:41 #6
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If you really wanted, you could build upon the suggestions to send an ajax request periodically. This could be combined with additional client side logic which listens for mousemove and keydown events. Going for an extended period of time without either of these events occurring could be used as a pretty decent indication that the user is not actively using the webpage, and has likely walked away/doing something else.
-
Jul 30, 2009, 19:49 #7
- Join Date
- Jul 2008
- Posts
- 81
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yeah I just went with the "being inactive for 10-15 minutes make then go bye bye" code. It was just the simplest way although it doesn't count users who just idle on the page. Thanks for all the suggestions guys.
-
Jul 30, 2009, 23:54 #8
- Join Date
- Sep 2005
- Location
- Sydney, NSW, Australia
- Posts
- 16,875
- Mentioned
- 25 Post(s)
- Tagged
- 1 Thread(s)
Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">
Bookmarks