23

When the user goes history-back-1...how do I detect that? And then, alert "the user clicked back!"

Using binds (and jQuery preferably)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 5
    Alex, you just asked a similar question at http://stackoverflow.com/questions/2008765/in-javascript-perferably-jquery-how-do-i-add-something-to-the-url-when-the-user Don't spam. – Justin Johnson Jan 05 '10 at 20:25
  • Possible duplicate of [In Javascript, preferably JQuery, how do I add something to the URL when the user clicks "back" button in the browser?](https://stackoverflow.com/questions/2008765/in-javascript-preferably-jquery-how-do-i-add-something-to-the-url-when-the-use) – Brian Tompsett - 汤莱恩 Aug 25 '19 at 17:25

5 Answers5

29

You generally can't (browser security restriction). You can tell if the user navigates away from the page (onbeforeunload, onunload fire) but you can't tell where they went unless you've set up your page to allow it.

HTML5 introduces the HTML5 History API; in conforming browsers, the onpopstate event will fire if the user navigates back to an earlier "page" on your site.

EricLaw
  • 56,563
  • 7
  • 151
  • 196
  • What about this? http://stackoverflow.com/questions/10462511/is-there-a-way-using-jquery-to-detect-the-back-button-being-pressed-cross-browse – Timo Huovinen Oct 08 '13 at 19:49
  • 3
    You can construct pages in such a way that hitting back is detectable, but the only way you can know where they went is if they happened to navigate to another page on your site (where you control the JavaScript). How to construct such a scenario? When the user visits "PageA.htm" you immediately send them to "PageB.htm" and if they load "PageA.htm" again, you can "guess" that they've hit "Back." – EricLaw Oct 08 '13 at 21:00
  • 1
    onpopstate will fire if you navigate forward as well – Luigi Jun 15 '22 at 10:52
8

try:

window.onbeforeunload = function (evt) {
  var message = 'Are you sure you want to leave?';
  if (typeof evt == 'undefined') {
    evt = window.event;
  }
  if (evt) {
    evt.returnValue = message;
  }
  return message;
}
Adnan
  • 25,882
  • 18
  • 81
  • 110
5
window.onpopstate=function()
{
  alert("Back/Forward clicked!");
}
Mohsen Haeri
  • 337
  • 4
  • 8
0

Following are the steps to detect back button click:

  1. Register a mouse down event on body $('body').on('mousedown', 'on all li');
  2. Now set a variable when mousedown event occur.
  3. Check this variable when your location changes.

IF variable changes to true it means list clicked otherwise back button.

This work in my use case. This solution may help others because it depends on app design.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Saurabh Ahuja
  • 473
  • 3
  • 13
-3

On the page you are looking at, you can add this piece of code to the onLoad event to move them back the page they were on.

if(history.length>0)history.go(+1)

If you want the alert then make it

if(history.length>0)alert("the user clicked back!")
Miranda
  • 81
  • 10
  • Won't work for anything. `history.length > 0` will give you true even when its a brand new page. It gives you the [entire length of history](http://stackoverflow.com/a/9757655/632951) **including** the backwards and forwards pages. – Pacerier Apr 06 '15 at 07:20