How to determine how long a user has been on a certain page?
I'm trying to figure out a way I can determine how long a user has been on a page. I'm open to javascript solutions that call some server script periodically, but I'm wondering if there are any better solutions than that?开发者_如何学编程
I've read Can you fire an event in JavaScript before the user closes the window? and it seems useful, but I need to know for certain when the page is closed.
Is there any way I can determine if the page is "active"? (meaning on top of other tabs / windows and the user is paying attention to it? this is for an educational web application)
I don't know how much this will be helpful.
However your problem seems more a browser task then a page task. If you want to implement a general, client side soltution you could write for example a chrome-extension.
In the chrome.tabs.*
api there are all function needed to detect when a page is loaded, active, selected or is closed.
onCreated
chrome.tabs.onCreated.addListener(function(Tab tab) {...}));
Fires when a tab is created.
onRemoved
chrome.tabs.onRemoved.addListener(function(integer tabId, object removeInfo) {...}));
Fires when a tab is closed.
onSelectionChanged
chrome.tabs.onSelectionChanged.addListener(function(integer tabId, object selectInfo) {...}));
Fires when the selected tab in a window changes.
Ended up creating a javascript function which called a php script on the server periodically. Saved the 'last-updated' field in a database and set a minimum time before which it could be updated in order to ensure the polling function wasnt maliciously called by a user.
精彩评论