如何检测用户是否单击了“后退”按钮

时间:2023-01-27 15:01:14

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

当用户进入历史记录1时...我该如何检测到?然后,提醒“用户点击回来!”

Using binds (and jquery preferably)

使用绑定(最好是jquery)

5 个解决方案

#1


20  

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.

你一般不能(浏览器安全限制)。您可以判断用户是否离开了页面(onbeforeunload,onunload fire),但除非您已将页面设置为允许,否则您无法分辨他们去了哪里。

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.

HTML5引入了HTML5 History API;在符合浏览器的情况下,如果用户导航回您网站上较早的“页面”,onpopstate事件将会触发。

#2


6  

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;
}

#3


2  

window.onpopstate=function()
{
  alert("Back/Forward clicked!");
}

#4


0  

Following are the steps to detect back button click

以下是检测后退按钮单击的步骤

  1. Register a mouse down event on body $('body').on('mousedown' ,'on all li' );

    在body $('body')上注册鼠标按下事件.on('mousedown','on all li');

    2.Now set a variable when mousedown event occur.

    2.当mousedown事件发生时,现在设置一个变量。

    3.Check this variable when your location changes.

    3.当您的位置发生变化时,请检查此变量。

IF variable chages to true it means list cliked otherwise backbutton

如果IF变量变为真,则意味着列表会以其他方式返回

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

这项工作在我的用例中。这个解决方案可以帮助其他人,因为它取决于应用程序设计

#5


-2  

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.

在您正在查看的页面上,您可以将这段代码添加到onLoad事件中,以将它们移回到它们所在的页面上。

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

If you want the alert then make it

如果你想要警报,那就去做吧

if(history.length>0)alert("the user clicked back!")

#1


20  

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.

你一般不能(浏览器安全限制)。您可以判断用户是否离开了页面(onbeforeunload,onunload fire),但除非您已将页面设置为允许,否则您无法分辨他们去了哪里。

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.

HTML5引入了HTML5 History API;在符合浏览器的情况下,如果用户导航回您网站上较早的“页面”,onpopstate事件将会触发。

#2


6  

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;
}

#3


2  

window.onpopstate=function()
{
  alert("Back/Forward clicked!");
}

#4


0  

Following are the steps to detect back button click

以下是检测后退按钮单击的步骤

  1. Register a mouse down event on body $('body').on('mousedown' ,'on all li' );

    在body $('body')上注册鼠标按下事件.on('mousedown','on all li');

    2.Now set a variable when mousedown event occur.

    2.当mousedown事件发生时,现在设置一个变量。

    3.Check this variable when your location changes.

    3.当您的位置发生变化时,请检查此变量。

IF variable chages to true it means list cliked otherwise backbutton

如果IF变量变为真,则意味着列表会以其他方式返回

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

这项工作在我的用例中。这个解决方案可以帮助其他人,因为它取决于应用程序设计

#5


-2  

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.

在您正在查看的页面上,您可以将这段代码添加到onLoad事件中,以将它们移回到它们所在的页面上。

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

If you want the alert then make it

如果你想要警报,那就去做吧

if(history.length>0)alert("the user clicked back!")