Window close in javascript - javascript

I need to end a user session only in case when user closes the window. I want to retain session if the user refreshes the page or hard reload the page(chrome) with ctrl+shift+r.
I have read post on this forum about usage of window.unload method but this method is called also in case of user refreshes the page with F5 or hard reload it or keep his cursor in address bar and hit enter. I only want to end the user session in case he close a tab or window.
Thanks,
Hitesh

You can not do it in pure HTML5. This is a security feature of modern navigators.

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.

onbeforeunload any way to know if it is reload or leave?

We have some forms that users fill out. If during the process the navigate away I need to perform an action.
I've found many examples of combining onbeforeunload and unload in the browser to prompt the user the user and remind them that if they close or navigate away the information they have entered won't be saved. That part is working well.
The only problem I still have is if the user reloads -- right-click reload, F5, or browser refresh button -- then the unload is also called. This is a problem for us.
I found information about the performance.navigation property. But the values of that aren't available until after the page reloads, that is, it tells me that the page I'm on has been reloaded, but not that the page is about to be reloaded.
Is there any way to detect an impending refresh as opposed to a navigate away?
No.
I recommend you "perform an action" as your user is filling out the form. Then if the user chooses to leave, no harm done.
It's perfectly feasible to post this information to a server as it is being populated. If the "perform an action" requires interaction with the user, you should just do that interaction on beforeunload.
EDIT: At the point of beforeunload, the only interaction that can be provided is an alert() asking the user to OK / Cancel navigating away from the page.

What's window.onunload?

Im a beginner and i see that line of code a lot on javascript files , for example :
window.onunload=function(){};
when should i use this and what is it role exactly ?
thank you .
This function gets called when the user closes the browser or navigates away from the page.
https://developer.mozilla.org/en/DOM/window.onunload
You also might want to check out onbeforeunload, which allows you to prompt the user with a confirmation message before leaving the page. This can be useful for reminding the user to save their changes, or making sure the user doesn't actually want to claim their free iPad 2.
onunload is an event that is triggered when the user navigates away from your page, or when the page is "unloaded".
It's triggered when a user follows a link, or closes the tab. It's used for clean up. Like saving a user's data when they leave the page. Usually it's paired with onbeforeunload (which is called before onunload is using the same criteria) to warn a user that they have unsaved data.
if a page has an onunload handler, browsers that restore the page state (remembering changed form field values, script environment) when you navigate away and back to the page do not-
that is, they load the page as if it was the first time it was opened, with no user applied changes.

Browser window close and tab close

I want to create a JS function that detect only browser close and tab close functionality.
I used this code from http://ykyuen.wordpress.com/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/
I works for me but when i place a cursor on address and hit enter it goes to kill page method. I need only to show alert when tab or browser is closed.
I am afraid this is not possible.
You cannot really make a distinction. When the user navigates away from your page in any way, you get the same events (beforeunload, unload). You can do some tricks (like in the link posted) whether the user clicked any of the links on your page or submitted one of your forms, but you cannot really differentiate between the refresh button, back button, the user entering a new address, the user clicking a bookmark, close tab, close browser, etc.

JavaScript problem toolbar=no

I have a simple logon page. When the user is validated, the window navigates to a new page. The javascript is window.open('http://www.google.com',"mytest",'toolbar=no'); My expectation is that when it navigates away from our logon page and opens the google site that the back button would be disabled. But it's not. Does anyone have any idea why?
It depends on your browser. Ultimately, all you can do with javascript's window.open() is tell the browser what you'd like it to do, but it's not obligated to do it. Browsers can and do ignore some directives based on user preferences.
I believe the option your looking for is 'location=no', as that hides the address bar and therefore the back button too. The toolbar is things like favorites/etc.
This is bad practice - what happens if the user has javascript disabled? If the browser prevents the js from removing the toolbar of the main window?
Instead, amend the logon page to detect whether the user is logged in before showing the login form. If logged in, show a message saying so instead of the form - that way, a user clicking back won't be a problem.
I find it very annoying when a website messes around with my browser window, and generally don't come back.
This is what worked for me. Instead of disabling the back key. I listen for on unload event. I then write the following in javascript:
window.onbeforeunload = function () { return "You should not press the back button while in this application. If you continue, your work will not be saved and you will need to log back in."}
Java Script pops a dialogue box with OK and Cancel options. If the user clicks cancel. The application stays right where they are. The script is embedded within the tags. For me this is the ideal solution. I found this at
http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

Categories