How do I handle user leaving/closing the page effectively? [duplicate] - javascript

This question already has answers here:
Intercept page exit event
(4 answers)
Closed 9 years ago.
Okay, I have a live application, it runs a messenger and I am trying to find the most reliable way to determine if a user has navigated away from the page or closed the window, et cetera. I have already implemented this:
window.onbeforeunload = function(event){ /* my code */ };
But that isn't always very reliable, sometimes it will fire and sometimes not. Is there a more reliable way of determining user presence or lack thereof?

Have the browser ping the server every X minutes. If you're making a chat client you'll likely have all the infrastructure you need already.

Related

Find when Javascript is paused (hard to summarize) [duplicate]

This question already has answers here:
Start calling js function when PC wakeup from sleep mode
(1 answer)
Can any desktop browsers detect when the computer resumes from sleep?
(6 answers)
Closed 8 years ago.
I'm making a little game out of Javascript, and I'm a little stuck. The game is basically one of those virtual pets, where you have to feed and do stuff with the pet. Now, I have a hunger system made were every 3 seconds the pet gets a little more hungry. The thing is, when I close my laptop, Javascript isn't running. What I'm doing now is getting the date of the last save and getting the difference of that with the current date when I open my laptop.
However, I have no way of triggering the above. I need an event that recognizes when the page comes back up the next time.
If you still don't understand what I'm trying to say, here's an example:
I open up a page and I close my laptop. Javascript has stopped. 30 minutes later, I open it back up and now I get an alert with how long I've been gone. What event can trigger the alert?
BTW, I have tried using such things as onload, onpageshow, etc. All of them either trigger once if not at all.
In advice you to put the current date often (like every 2 seconds). And when you load the page, you refer to this data for seeing how long you're been gone.
http://www.w3schools.com/jsref/event_onload.asp
You do know there is an focus event on the window don't you?
I think it will do what you want
window.addEventListener("focus", function(event) {
// do your thing
}
check this fiddle for an example
http://jsfiddle.net/whQFz/

detect closing current tab on browser and call ajax function [duplicate]

This question already has answers here:
JavaScript, browsers, window close - send an AJAX request or run a script on window closing
(9 answers)
Closed 6 years ago.
I am working on a website, where at a time one admin can login. I implemented this by saving value in database. Now the problem is when current admin forget to logout, the value in database do not change and another admin can't login again. Even this current admin can't login later on, cause the value in database is checked in condition.
I have looked in window.onbeforeload and window.onunload functions, but it only trigger on page refresh etc, not detecting browser close tab/window. I want to detect closing browser tab and call function upon close, so I can change value in database using ajax.
Any help will be appreciated! Thanks
The event you are looking for is onbeforeunload
Use window.uneforeunload which is trigged when tab/window is closed.
Updated
window.onbeforeunload = function (){
// update your database from here before page closed
}
DEMO

How to track user hits back button on the browser [duplicate]

This question already has answers here:
Track when user hits back button on the browser
(7 answers)
Closed 9 years ago.
I want to track event which fire on, when user hits back button on the browser.
I found many post related this but all suggest use of .onbeforeunload event. but this event also fire on, when page is refresh or browser window is closed.
If there is any idea to track only browser back event.
Thanks for your help...!!!
You can use History.js:
http://balupton.github.io/history.js/demo/
History.js gracefully supports the HTML5 History/State APIs
(pushState, replaceState, onPopState) in all browsers

Notification When Chrome Exits [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Event onBrowserClose for Google Chrome?
Is it possible to detect when the user exits the Chrome browser, so I can process some data right before they exit? I have looked into using chrome.windows.onRemoved.addListener(function(integer windowId) {...}); but it only listens for a window and not the entire browser.
Well, no.
There is no Close event, and Chrome doesn't guarantee that all others pending events will be fired before closing (will be fired at all).
I personally researched this topic while writing the "History Eliminator" extension, that would erase your browser history on close.

Cross browser window close event [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Close /Kills the seesion when close the browser or tab
Is there a solution for cross browser event. I need to check if user closes their window and to throw an ajax request to my database to sign them out.
I've looked everyone but most cases its not working in all browsers. Anyone have a solution? Or Alternative on how to do this perhaps a conditional statement depending on the browser?
Thanks!
I don't think such a thing exists. You can try an onunload, but that also fires when you refresh the page. This question has been the bane of many that want to do what you're asking for.

Categories