showing results for - "detect back button press javascript"
Raphael
25 Feb 2019
1So as far as AJAX is concerned...
2
3Pressing back while using most web-apps that use AJAX to navigate specific parts of a page is a HUGE issue. I don't accept that 'having to disable the button means you're doing something wrong' and in fact developers in different facets have long run into this problem. Here's my solution:
4
5window.onload = function () {
6    if (typeof history.pushState === "function") {
7        history.pushState("jibberish", null, null);
8        window.onpopstate = function () {
9            history.pushState('newjibberish', null, null);
10            // Handle the back (or forward) buttons here
11            // Will NOT handle refresh, use onbeforeunload for this.
12        };
13    }
14    else {
15        var ignoreHashChange = true;
16        window.onhashchange = function () {
17            if (!ignoreHashChange) {
18                ignoreHashChange = true;
19                window.location.hash = Math.random();
20                // Detect and redirect change here
21                // Works in older FF and IE9
22                // * it does mess with your hash symbol (anchor?) pound sign
23                // delimiter on the end of the URL
24            }
25            else {
26                ignoreHashChange = false;   
27            }
28        };
29    }
30}
31As far as Ive been able to tell this works across chrome, firefox, haven't tested IE yet.
queries leading to this page
detect back button click javascriptdetect on back keyboard clicked javascripthow to detect if back button is pressed javascripthow to detect browser back button eventdetect back button clickcheck if back button is pressed javascriptjs browser check back button clickedjavascript detect browser back button clickdetect back button click on browserjavascript detect back button is pressed check if browser back button is clicked javascriptcheck when browser back button clickedjavascript 2bdetect back buttonjavascript detect browser back buttonhow to detect back button of browser in jsjs detect back button browser clickhow to detect browser back button click in javascriptjs detect back buttondetect back button javascriptback button detection javascripthow to find browser back button click in javascriptjavascript detect back buttondetect back button browser javascriptdetect javascript browser back buttonhow to detect pressed back button click in javascriptdetect back button click in browser and do something elser javascriptdetect back button in javascriptjavascirpt detect back button clickhow to detect back button pressed in browser using javascripthow to detect browser back button event in jsdetect browser back button click using javascripttell if browser back is clicked in javascriptdetect back button jsjs detect back button browswrhow to detect user click back button jsdetect browser back button pressjs detect back button pressdetect browser back button javascripthow to detect browser back button event in javascriptdetect back button press javascriptjs detect back button clickhow to detect back button click in javascriptdetect if browser back button clicked jsdetect back button press javascript