How to detect if the user clicked the "back" button
Asked Answered
C

6

24

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

Using binds (and jQuery preferably)

Cheesecake answered 5/1, 2010 at 20:21 Comment(2)
Alex, you just asked a similar question at #2009265 Don't spam.Gram
Possible duplicate of In Javascript, preferably JQuery, how do I add something to the URL when the user clicks "back" button in the browser?Economy
F
32

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.

Fortunna answered 5/1, 2010 at 20:25 Comment(3)
What about this? #10463011Redbreast
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."Fortunna
onpopstate will fire if you navigate forward as wellSurplus
W
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;
}
Willed answered 5/1, 2010 at 20:26 Comment(2)
OnBeforeUnload will also fire if the user goes to some other site.Fortunna
is there any way to not display the alert message?Ursuline
A
5
window.onpopstate=function()
{
  alert("Back/Forward clicked!");
}
Angelus answered 29/3, 2013 at 12:38 Comment(1)
This will not fire if you visit another page and then come back to this page.Ferritin
K
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.

Kero answered 22/9, 2015 at 5:39 Comment(1)
what about the browser next and refresh buttons?Hindman
B
0

hide loading onload or when using back button:

        jQuery(document).ready(function ($) {
            $.minicart.hideLoading();
        });
        window.addEventListener('load', function () {
            jQuery(document).ready(function ($) {
                $.minicart.hideLoading();
            });
        });
        window.onload = function () {
            $.minicart.hideLoading();
        }
Bowknot answered 11/9, 2023 at 15:25 Comment(0)
C
-4

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!")
Confessional answered 1/10, 2013 at 18:29 Comment(1)
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 including the backwards and forwards pages.Ferritin

© 2022 - 2024 — McMap. All rights reserved.