19

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,438
  • 68
  • 55
  • 126
TIMEX
  • 238,746
  • 336
  • 750
  • 1,061
  • 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

25

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
  • 55,769
  • 7
  • 146
  • 189
  • 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
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
  • 24,852
  • 17
  • 77
  • 108
6
window.onpopstate=function()
{
  alert("Back/Forward clicked!");
}
Mohsen Haeri
  • 162
  • 2
  • 7
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 chages to true it means list cliked otherwise backbutton

This work in my usecase .This solution may helps others because it depends on app design

KANAYO AUGUSTIN UG
  • 1,877
  • 1
  • 14
  • 27
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
  • 79
  • 9
  • 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