How to automatically logoff user whenever the user closes the tab - javascript

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.

Related

Capturing the browser tab reload and tab close functionality

I want to call an ajax to other pages on the browser or tab close. When we reload tab or close tab it calls onbeforeUnload and onUnload events.
If I reload page either by pressing ctrl+r or by pressing enter in address bar it should reload page without any prompt and if I click close of browser or tab or I press ctrl+w keys it should prompt that "Changes you made might be lost" and if user click leave it should close tab and call an ajax, else it should stay on page.
Can anyone help me with this?
Thanks in advance
Most of the browsers intentionally block popups triggered in onbeforeunload, for known reasons.
But if you want to preserve users' data from erasing, you can engage window.localstorage property.

What are all the possible causes of firing onbeforeunload event?

I am writing some code to track user behavior on my website in order to improve conversion rate by determining how users are interacting with my website.
This requires me to send some events like "Leaving Website" to Google analytics inside the unbeforeunload handler. I have a few questions about the firing of this event.
Does it fire if user switches tabs without opening a new link?
Does it fire if a new tab is opened after users clicks a link with target="_blank" on my website?
Does it fire if a users visits some other page of my own website?
Does it fire on page reload?
Are there any other instances where it fires?
Thanks.
onbeforeunload will fire right before a webpage is 'unloaded' in the current tab (By navigating to a new page), or the current tab is closed. This means:
When a user navigates to a new page on or off your website.
When a user closes the tab or browser
When a user refreshes a page
When a user submits a form
To answer your questions
Does it fire if user switches tabs without opening a new link?
No
Does it fire if a new tab is opened after users clicks a link with target="_blank" on my website?
No
Does it fire if a users visits some other page of my own website?
Yes
Does it fire on page reload?
Yes
The current version of analytics.js should (Using sendBeacon()) dispatch events to GA as a POST request, which should be honoured even when closing tabs or the browser.
However, I wouldn't be so sure that 'Leaving website' is appropriate. Perhaps something more like 'Leaving page'.

Detect browser closed event only

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?

Fire onbeforeunload only when window is closed (includes working example)

I want to fire the onbeforeunload event only when the tab or browser is closed and not upon refresh, link click and so on. I found a site where that works: https://checkout.deindeal.ch/
Steps to fire the event in Chrome:
Put something into the cart
Click on "Zur Kasse"
Close tab
onbeforeunload Message is shown and a popup opens. onbeforeunload events like refresh will not fire. I debugged their code, but could not find the place where they exclude those events or whatsoever.
Anyone out there who can find the correct place in the code?
Thanks!
For readers that want to use onbeforeunload,
The event onbeforeunload should be used for preventing user's data loss, like filled in forms and other data that are not saved and will be lost upon page refresh.
Using onbeforeunload to prevent a user from leaving your page is annoying and will not stop the user from leaving.
You can not detect a browser tab refresh button press, unless the user use a shortcut that you can detect with key press (like Ctrl+F5).
More info here :
javascript beforeunload detect refresh versus close
Old answer was removed in order not to mislead anyone (it was not working on nowadays browsers).
More stack overflow links related on onbeforeunload use:
How can i get the destination url in javascript onbeforeunload event?
How to prevent calling onbeforeunload when page refresh
Fire onbeforeunload confirm alert for external sites only

How to allow popups when url loads if it is NOT a physical click by the user in js

I want to allow popups when site loads and if the root cause of an event is NOT a physical click by the user, then it's getting blocked. any help
My code:
$("#buttonBtn").on('click',function(){
openpopup('temp'+id)
});
$("#buttonBtn").trigger('click');
Thanks in advance
With this, if you explicitly click, popup appears. If user comes to this button via keyboard and press Enter or something, I guess this would not be triggered.
$("#buttonBtn").click(function(e){
e.preventDefault();
// some job you will do if clicked, for example, open popup
});
Great example here
You can generally open popups by using window.open. However, most browsers have built-in popup blocker where the user will be notified about a popup and will be asked if he really wants to show it.
The MDN has an informative article about window.open. It also gives some information about popup blockers:
How can I tell when my window was blocked by a popup blocker?
With the
built-in popup blockers of Mozilla/Firefox and Internet Explorer 6
SP2, you have to check the return value of window.open(): it will be
null if the window wasn't allowed to open. However, for most other
popup blockers, there is no reliable way.
Source: https://developer.mozilla.org/en-US/docs/DOM/window.open

Categories