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

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

问题描述

我有一个网页 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 中的所有字段都将被清除,并且必须重新输入所有内容.我无法在会话中保存发布的信息,我正在尝试了解如何让 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 will 自动在后退按钮点击时保留表单内容,只要:

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.)

如果您从脚本加载创建自己的表单域,那么浏览器无法知道新的输入控件与旧的实例相同",因此无法用以前的输入控件填充它-提交的价值.在这种情况下,如果您希望它与后退按钮一起使用,您必须开始在客户端上存储数据.

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天全站免登陆