Is there a simple way to detect if the visitor is still viewing a page on a web page that does not require p开发者_如何学运维olling the server every X seconds?
If there was something less taxing on the server, that would be ideal.
The technical solution proposed in the other answers are fine, as far as they go, but I wonder how well they answer the underlying issue.
Right now, I have 4 Safari windows open, with a total of 17 tabs; and a Goggle Chrome one, with 4 tabs. Not sure exactly why I don't currently have any Firefox open (probably just forgot to restart it after upgrading to 3.5.4 recently) but normally there would be 2-3 windows there with another 8-10 tabs. Am I "still viewing" these 30 or so web pages -- at the same time...? Technically, I guess every programming-based solution you'll find will say that, why, yes, of course I am... does that make ANY sense to you?!
In real life, I may (perhaps, unless I'm doing something else again on my computer) be viewing ONE of those web pages at a given time -- maybe 2-3 if I have multiple or very large screens, but generally, 1 is the real-life-sensible answer. Which one? You're not going to find out without substantial Javascript magic -- if then.
It's such a hard problem, that adding the weird constraint of not having the magical Javascript periodically tell the server "I'm still open and it looks like the user is viewing me [[some interaction has happened within the last XX seconds, or...]]" vs "I'm open but I suspect I'm in the background or the user has gone away to have lunch leaving the browser open because there has been no interaction since YYY seconds ago" vs "I'm being navigated away from" (and no message means the whole browser crashed or something) -- adding that weird constraint basically ensures the problem is totally insoluble.
If you truly care about whether the user is actually viewing the page, a periodic every-few-second Ajax interaction from Javascript back to the server is a miniscule price to pay, and really the least of your problems. If you don't truly care, it's different, of course. Maybe tell us what actual problem you're really trying to solve...?!
Use
Reverse AJAX
Reverse Ajax refers to an Ajax design pattern that uses long-lived HTTP connections to enable low-latency communication between a web server and a browser. Basically it is a way of sending data from client to server and a mechanism for pushing server data back to the browser.
Comet (programming)
In web development, Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Comet is an umbrella term for multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as JavaScript, rather than on non-default plugins.
You could use a javascript on_exit event to fire off an ajax event, open a popup window with a tracker, or do something else similar. These only work if your user clicks away from your page, rather than closing the window.
the simplest (and he most lightweight in terms of traffic) would be to track mousemove/keyup events on the page. Within, say, 1 minute since the mouse was last moved you can be sure the user is actually doing something on the site (and not for example went for lunch leaving your window open).
精彩评论