Ho do I detect Browser close event only, ie. I have a web application which allows any user to be signed in only once. So if the user by mistake or purposely closes the browser my application does not allow it to login again as the flag which was being set during logging in was not cleared when user closes the browser. So I would like to detect closing of the browser and log him off if its detected.
I tried onbeforeunload, but this also fired when page is being redirected or browser url is changed. Is there a way by which I can only track browser closing or Browser URL change?
Related
So I am working on a testing application and I need to call a finsihTheTest() function (i.e. this function finishes the test by saving answers, time and other information) whenever following conditions occur:
User tries to reload page.
User tries to go back from the page.
User tries to close the tab.
User tries to close the browser window.
User goes to another url.
If anything happens that closes the page like laptop/PC shutdown, internet lost or anything else.
What I exactly want to do is, if once a user starts the test and by any mean he attempts to leave I want to save his state. Which is being done by the function finishTheTest().
I got a clue but it didn't work:
function UnLoadWindow() {
return 'We strongly recommends NOT closing this window yet.'
}
window.onbeforeunload = UnLoadWindow;
To get the full results for your cases there's many things you should now on how browsers react on many scenarios.
To understand more please read this section :
Especially on mobile, the unload event is not reliably fired. For example, the unload event is not fired at all in the following scenario:
A mobile user visits your page.
The user then switches to a different app.
Later, the user closes the browser from the app manager.
Also, the unload event is not compatible with the back/forward cache (bfcache), because many pages using this event assume that the page will not continue to exist after the event is fired. To combat this, some browsers (such as Firefox) will not place pages in the bfcache if they have unload listeners, and this is bad for performance. Others, such as Chrome, will not fire the unload when a user navigates away.
If you're specifically trying to detect page unload events, it's best to listen for the pagehide event.
window.addEventListener('pagehide', function(event) {
document.cookie = "saveData=test"
},false)
This way you can save your user current data and reload it on next page window load event
I need to be able to detect when a user re-opens their browser after they go to their homescreen by clicking the home button on a mobile phone.
Is there an event I can subscribe to or something?
EDIT: To make this extremely clear, I am looking for a solution based in the web, not a mobile app. I need a js event or something to detect when my website is reopened.
The unload event can be monitored to check if the page has been closed dues to navigation away from the page or specific user action has been take to close the page.
The beforeunload evemt is similar to unload but it may be possible to ask if the user wants to stay on the page. If you want to debug when this event occurs I would suggest saving a message in local storage and logging (or otherwise alerting) it in debug code when the page is loaded again.
The blur event can be used to see if the page has lost focus by checking the relatedTarget property of the event object - if focus has been transferred off page it will be null.
None of these can implicitly check if the user actually went to the home screen and came back, and I would consider it a security breach if you could tell exactly. The blur event can at least tell if the page has lost focus, but will fire in a desktop environment if the user clicks on, say, the address bar.
I have a download link in my application. If user clicks on the link, then a default open save dialog box is popped up in a new window in IE8. Also, user can't navigate anywhere till he clicks on open or save on this prompt.
But, the open save dialog is different and comes at bottom of the page in IE11 as given below. And also user can navigate anywhere in the application without responding to this save prompt. The problem is that this prompt is still there even after the user logouts from the application and can open or save even after logout.
Is there any way to force the user to respond to open save dialog and then only navigate to other pages in IE11 or force dialog to close if user logs out ?
IE8
IE11
Unfortunately no, this is just the browser behavior and it cannot be altered, for good reason too. I would not want websites to have any control over download dialogue boxes that my browser presents to me.
If your goal is to prevent a user from downloading a file unless they are still logged in, you may serve the file via a script on your server, which first checks the login status before sending the file to the browser/user.
I am working on dotnetnuke website. I want to logoff the user when he closes the browser or tab. Please help me to resolve this. How can I detect browser close event and how can logout the user.
You can't detect the browser close or tab close using javascript.
Only you can know the page unload, the following events are OnBeforeUnload &
OnUnload
Neither of these events can tell you the way that you closed the page.
I remember that facebook did something similar,
Lets say you loaded facebook.com, browsed around a bit and then opened a new tab to read some news, meanwhile you had updates to your facebook feed, but they would not be automatically displayed when you switched back to the facebook tab, only when you switched to the facebook tab they would then fire the event for fetching the feed updates.
How is this done?
It can by done by detecting in javascript if the browser window gained focus.
Dynamic changes on the page or ajax calls are probably done only when the browser window has focus. More about detecting browser window focus in javascript:
Is there a way to detect if a browser window is not currently active?