我可以检测到用户使用后退按钮访问页面吗? [英] Can I detect when a user gets to a page using the back button?

查看:116
本文介绍了我可以检测到用户使用后退按钮访问页面吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我真正需要知道的是,如果有任何javascript事件,当用户通过后退按钮到达页面,将可靠地触发。我为 body 元素尝试了 onload 事件,但它不会在Firefox或Safari上触发。






我正在使用一些旧代码,试图通过禁用所有表单提交按钮来阻止双重提交表单数据用户单击一个(通过侦听表单的onSubmit事件)。这个代码应用于应用程序中的每个表单,不管双重提交是否会成为一个问题。



问题是,如果用户点击返回按钮Firefox或Safari,所有的提交按钮仍然被禁用,当他到达页面。在Firefox上,他们甚至在刷新后被禁用(只有Ctrl + F5刷新会这样做)!



我试过听 onLoad 事件,但是FF和Safari不会触发,当你通过后退按钮到达页面。 (有趣的是,即使按钮处于停用状态,也会在FF上进行软刷新)。






简单的HTML文件,将显示我的意思:

 < html>< body& 
< form name =theFormid =theFormaction =test2.html
onSubmit =document.theForm.theButton.disabled = true;>
< input id =theButtontype =submitname =theButtonvalue =点击我! />
< / form>
< / body>< / html>注意:我在WinXP上测试了IE8,FF3.5,Safari 4,Opera 9,和Chrome 2.0。

解决方案

我不认为你可以测试对于后退按钮专门,但我相信你可以检查浏览器的历史位置与Javascript。


Edit: What I really need to know is if there is any javascript event that will reliably fire when the user arrives at the page via the back button. I tried the onload event for the body element, but it doesn't fire on Firefox or Safari.


I'm working with some old code that tries to prevent double-submission of form data by disabling all form submission buttons as soon as the user clicks one (by listening to the form's onSubmit event). This code is applied to every form in the app, regardless of whether double-submission would even be a problem.

The problem is that if the user hits the back button in Firefox or Safari, all the submit buttons are still disabled when he gets to the page. On Firefox, they are even disabled after a refresh (only a Ctrl+F5 refresh will do it)!

I've tried listening to body's onLoad event, but FF and Safari don't fire that when you get to the page via back button. (Interestingly, it is fired on a soft refresh on FF, even though the button stays disabled.)


Here's a very simple HTML doc that will show what I mean:

<html><body>
<form name="theForm" id="theForm" action="test2.html"
      onSubmit="document.theForm.theButton.disabled = true;">
  <input id="theButton" type="submit" name="theButton" value="Click me!" />
</form>
</body></html>

Note: I've tested on WinXP with IE8, FF3.5, Safari 4, Opera 9, and Chrome 2.0. Only Safari and FF leave the button disabled when you use back to get to them.

解决方案

I don't think you can test for the back button specifically, but I believe you can check the browser's history position with Javascript.

这篇关于我可以检测到用户使用后退按钮访问页面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆