浏览器后退按钮可恢复空字段 [英] Browser back button restores empty fields

查看:113
本文介绍了浏览器后退按钮可恢复空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页 x.php (在我的网站的密码保护区域内),其中有一个表单和一个使用 POST 发送表单数据的方法并打开 x.php#abc 。这很好用。

I have a web page x.php (in a password protected area of my web site) which has a form and a button which uses the POST method to send the form data and opens x.php#abc. This works pretty well.

但是,如果用户决定在Internet Explorer 7中导航回来,原始 x.php <中的所有字段/ code>清除并且必须再输入所有内容。我无法在会话中保存发布的信息,我试图了解如何让IE7按照我想要的方式运行。

However, if the users decides to navigate back in Internet Explorer 7, all the fields in the original x.php get cleared and everything must be typed in again. I cannot save the posted information in a session and I am trying to understand how I can get IE7 to behave the way I want.

我在网上搜索并找到了表明HTTP标头应包含显式缓存信息的答案。目前,我试过这个:

I've searched the web and found answers which suggest that the HTTP header should contain explicit caching information. Currently, I've tried this :

session_name("FOO");
session_start();
header("Pragma: public");
header("Expires: Fri, 7 Nov 2008 23:00:00 GMT");
header("Cache-Control: public, max-age=3600, must-revalidate");
header("Last-Modified: Thu, 30 Oct 2008 17:00:00 GMT");

及其变体。没有成功。使用 WireShark 等工具查看返回的标题,可以看出Apache确实很尊重我的标题。

and variations thereof. Without success. Looking at the returned headers with a tool such as WireShark shows me that Apache is indeed honouring my headers.

所以我的问题是:我做错了什么?

So my question is: what am I doing wrong?

推荐答案

IE 会自动在后退按钮上保留表单内容,只要:

IE will retain form contents on a back button click automatically, as long as:


  • 你没有破坏缓存使用no-cache pragma或类似的

  • 相关的表单字段不是由脚本动态创建的

你似乎手头有缓存,所以我猜测后者可能适用。 (正如mkoeller所说,如果页面在最后几次后退点击中,通过保持页面本身的活动时间长于屏幕上的时间,Firefox可以避免此问题。但这是可选的,Firefox将恢复与IE和其他相同的行为浏览器一旦你浏览了几页,它已经过期了。)

You seem to have the cacheing in hand, so I'm guessing the latter may apply. (As mkoeller says, Firefox avoids this problem if the page is in the last few back clicks by keeping the page itself alive for longer than it's on the screen. However this is optional, and Firefox will revert to the same behaviour as IE and other browsers once you've browsed a few pages ahead and it has expired the old one.)

如果你是从脚本onload创建你自己的表单字段,那么浏览器无法知道新输入控件与旧实例相同,因此无法使用先前提交的值填充它。在这种情况下,如果你希望它与后退按钮一起运行,你必须开始在客户端上存储数据。

If you're creating your own form fields from script onload, then the browser has no way of knowing that the new input control is ‘the same’ as the old instance, so it can't fill it in with the previously-submitted value. In this case if you want it to play well with the back button you have to start storing data on the client.

然后你必须使用某种状态键控这样每个数据集只与页面的一个实例相关联,否则通过同一表单的多个实例或同时在表单上打开两个浏览器选项卡会严重混淆您的脚本。

Then you have to use some sort of state-keying so that each set of data is tied to exactly one instance of the page, otherwise going through multiple instances of the same form or having two browsers tabs open on the form at once will severely confuse your script.

然后然后如果它们是大型表单,你开始收集大量数据,如果你使用的客户端存储机制是cookie,你可以开始丢失数据,以及每个HTTP请求发送一堆不必要的状态废话。其他客户端存储机制可用,但它们是特定于浏览器的。

And then you are starting to collect a lot of data if they're big forms, and if the client-side storage mechanism you're using is cookies you can start to lose data, as well as sending a load of unnecessary state nonsense with every HTTP request. Other client-side storage mechanisms are available but they're browser-specific.

简而言之:很好地执行动态生成的表单是一个巨大的痛苦,如果您可以。在页面上有一个隐藏的表单,脚本可以看到,从而允许浏览器执行他们的字段记忆 - 魔术而不是给你任务,通常要容易得多。

In short: doing dynamically-generated forms nicely is a huge pain and it's probably best avoided if you can. Having a hidden form on the page that a script makes visible, thus allowing browsers to do their field-remembering-magic instead of giving you the task, is typically much easier.

这篇关于浏览器后退按钮可恢复空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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