跟踪用户何时点击浏览器上的后退按钮

2021-12-25 00:00:00 browser php javascript

是否可以检测到用户何时点击了浏览器的后退按钮?

我有一个 Ajax 应用程序,如果我可以检测到用户何时点击了后退按钮,我就可以返回相应的数据

任何使用 PHP、JavaScript 的解决方案都是首选.地狱任何语言的解决方案都很好,只需要我可以翻译成 PHP/JavaScript 的东西

从下方剪切和粘贴:

哇,所有优秀的答案.我想使用 Yahoo,但我已经在使用 Prototype 和 Scriptaculous 库并且不想添加更多的 ajax 库.但它使用了 iFrames,这给了我一个很好的指针来编写我自己的代码.

解决方案

有多种方法可以做到,但有些方法只能在某些浏览器中使用.我最清楚的是在页面上嵌入一个几乎不可见的微小 iframe.当用户点击后退按钮时,iframe 会向后导航,您可以检测并更新您的页面.这里是另一种解决方案.>

您可能还想查看 Gmail 之类的源代码,看看他们是如何做到的.

这里是 一个库,可以满足您的需求方式

Is it possible to detect when the user clicks on the browser's back button?

I have an Ajax application and if I can detect when the user clicks on the back button I can display the appropriate data back

Any solution using PHP, JavaScript is preferable. Hell a solution in any language is fine, just need something that I can translate to PHP/JavaScript

Edit: Cut and paste from below:

Wow, all excellent answers. I'd like to use Yahoo but I already use Prototype and Scriptaculous libraries and don't want to add more ajax libraries. But it uses iFrames which gives me a good pointer to write my own code.

解决方案

There are multiple ways of doing it, though some will only work in certain browsers. One that I know off the top of my head is to embed a tiny near-invisible iframe on the page. When the user hits the back button the iframe is navigated back which you can detect and then update your page. Here is another solution.

You might also want to go view source on something like gmail and see how they do it.

Here's a library for the sort of thing you're looking for by the way

相关文章