How do you definitively detect whether or not the user has pressed the back button in the browser?
How do you enforce the use of an in-page back button inside a single page web application using a #URL
system?
Why on earth don't browser back buttons fire their own events!?
Here's my take at it. The assumption is, when the URL changes but there has no click within the
document
detected, it's a browser back (yes, or forward). A users click is reset after 2 seconds to make this work on pages that load content via Ajax:The document.mouseover does not work for IE and FireFox. However I have tried this :
This works for Chrome and IE and not FireFox. Still working to get FireFox right. Any easy way on detecting Browser back/forward button click are welcome, not particularly in JQuery but also AngularJS or plain Javascript.
You can try
popstate
event handler, e.g:Note: For the best results, you should load this code only on specific pages where you want to implement the logic to avoid any other unexpected issues.
The popstate event is fired each time when the current history entry changes (user navigates to a new state). That happens when user clicks on browser's Back/Forward buttons or when
history.back()
,history.forward()
,history.go()
methods are programatically called.The
event.state
is property of the event is equal to the history state object.For jQuery syntax, wrap it around (to add even listener after document is ready):
See also: window.onpopstate on page load
See also the examples on Single-Page Apps and HTML5 pushState page:
This should be compatible with Chrome 5+, Firefox 4+, IE 10+, Safari 6+, Opera 11.5+ and similar.
In javascript, navigation type
2
means browser's back or forward button clicked and the browser is actually taking content from cache.(Note: As per Sharky's feedback, I've included code to detect backspaces)
So, I've seen these questions frequently on SO, and have recently run into the issue of controlling back button functionality myself. After a few days of searching for the best solution for my application (Single-Page with Hash Navigation), I've come up with a simple, cross-browser, library-less system for detecting the back button.
Most people recommend using:
However, this function will also be called when a user uses on in-page element that changes the location hash. Not the best user experience when your user clicks and the page goes backwards or forwards.
To give you a general outline of my system, I'm filling up an array with previous hashes as my user moves through the interface. It looks something like this:
Pretty straight forward. I do this to ensure cross-browser support, as well as support for older browsers. Simply pass the new hash to the function, and it'll store it for you and then change the hash (which is then put into the browser's history).
I also utilise an in-page back button that moves the user between pages using the
lasthash
array. It looks like this:So this will move the user back to the last hash, and remove that last hash from the array (I have no forward button right now).
So. How do I detect whether or not a user has used my in-page back button, or the browser button?
At first I looked at
window.onbeforeunload
, but to no avail - that is only called if the user is going to change pages. This does not happen in a single-page-application using hash navigation.So, after some more digging, I saw recommendations for trying to set a flag variable. The issue with this in my case, is that I would try to set it, but as everything is asynchronous, it wouldn't always be set in time for the if statement in the hash change.
.onMouseDown
wasn't always called in click, and adding it to an onclick wouldn't ever trigger it fast enough.This is when I started to look at the difference between
document
, andwindow
. My final solution was to set the flag usingdocument.onmouseover
, and disable it usingdocument.onmouseleave
.What happens is that while the user's mouse is inside the document area (read: the rendered page, but excluding the browser frame), my boolean is set to
true
. As soon as the mouse leaves the document area, the boolean flips tofalse
.This way, I can change my
window.onhashchange
to:You'll note the check for
#undefined
. This is because if there is no history available in my array, it returnsundefined
. I use this to ask the user if they want to leave using awindow.onbeforeunload
event.So, in short, and for people that aren't necessarily using an in-page back button or an array to store the history:
And there you have it. a simple, three-part way to detect back button usage vs in-page elements with regards to hash navigation.
EDIT:
To ensure that the user doesn't use backspace to trigger the back event, you can also include the following (Thanks to @thetoolman on this Question):
I tried the above options but none of them is working for me. Here is the solution
Refer this link http://www.codeproject.com/Articles/696526/Solution-to-Browser-Back-Button-Click-Event-Handli for more details