Trying to invalidate the session once click on the browser back button as well as browser close using jQuery.
How to capture the browser back button event and browser close event? Is there any advantage with respect to javascript?
Any help appreciated.
Hopefully this helps you:
http://api.jquery.com/unload/
The unload event is sent to the window element when the user navigates
away from the page. This could mean one of many things. The user could
have clicked on a link to leave the page, or typed in a new URL in the
address bar. The forward and back buttons will trigger the event.
Closing the browser window will cause the event to be triggered. Even
a page reload will first create an unload event.
Related
There is a number of threads here on StackOverflow concerning how to catch the back/forward button, but that's not what I want to do. I want to fire a method each time a page is loaded by pressing the back/forward button. So not on the page, where the user clicked back/forward, but on the page that the user got to after clicking back/forward.
I also tried looking at events using Chrome and I haven't found any event that is firing when coming back or going forward to a page.
You can use https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event
The popstate event will be triggered by doing a browser action such as a click on the back or forward button (or calling history.back() or history.forward() in JavaScript).
First you can save the info that user has navigated to localStorage or cookie, and then you can check if that localStorage entry or cookie exists on the next page load event.
If it exists, user has navigated to that page using back/forward buttons, you can do whatever you need to do, and clear the localStorage entry/cookie.
Is there any way to detect if user closing open window from close tab of browser in javascript except onbeforeunload function this occurs on page refresh, form submission, button click etc so this is not my solution.
You can track the mousemovement and if the mouse leaves the screen a user may leave your page. You have no 100% guarantee to detect if a user is closing a page.
On a certain page in my app, I have a popup guide that walks the user through several steps. When the user clicks the popup next button it advances, sometimes going to a new page (but not always). My problem is that when the user clicks the browser back button, the popup does not close and causes issues if the user tries to advance again. What i'd like to do is just close the popup if the user hits the browser back button. I'd assumed this would be an easy problem, but I can't figure out a solution.
Since this is a SPA, 'onbeforeunload' and 'onunload' don't fire. The way the app is setup, 'onpopstate' also doesn't fire. 'hashchange' fires, but it also fires sometimes when the user clicks the next button on the popup, and I don't see a way to differentiate between that hashchange and hashchange when the user clicks back.
Is there some other event I can check for? I just want to run a function to close my popup (and do cleanup) when the user clicks the back button.
The following assumes you do not use the history API:
Often times I have seen use of # (the anchor tag) in the URL. This will allow for navigation throughout a single page without refreshing the page. As the user progresses in the workflow. For example,
window.location.href = "http://stackoverflow.com/questions/36408418/how-can-i-detect-that-the-browser-back-button-has-been-pressed-in-a-single-page" + "#test"
will not refresh the page. When the back button is pressed, the following statement will evaluate to be true:
(window.location.href == "http://stackoverflow.com/questions/36408418/how-can-i-detect-that-the-browser-back-button-has-been-pressed-in-a-single-page")
My page has an affiliate pop-up whenever someone clicks anywhere on the page. This part works fine.
So then I created a script where if they haven't clicked on their own after a while it will simulate a click, thus triggering the pop-up. However, the browser can somehow tell that this is a fake click & auto-blocks the pop-up (but it works fine if you actually click on the page, the browser won't block the pop-up then).
I've tried various ways of simulating a real-world click in javascript but nothing works. Any ideas of how to stop it from blocking the pop-up?
You cannot a trigger false click event so that your popup is not blocked by the browser. This is a not possible.
Popups will only work if they originate from a trusted event that is an event that the user initiated. In your first case the user clicks on the page causing a trusted event which allows it to open. Your second case however the user has made no such action, so no trusted event and no popup.
What I mean is - when user clicks on some browser page reload/refresh button we do not want to reload page - we want to capture that event and call some function (for simple example one with some alert). I need it to work in IE6 and up and Chrome and Firefox3+ of course. How to do such thing (not using jQuery and other libs)?
It's not possible.
You can detect when an unload occurs, but you can't detect what caused the unload and you can't cancel it without the user's consent. For instance, typing a new address in the address box, clicking a link, submitting a form, selecting a bookmark or refreshing the page will all fire an onbeforeunload event.
You cannot. You can't change the operating system functionality from within any browser except IE.