防止Firefox在刷新之后恢复以前用户输入的状态 [英] Prevent Firefox from Restoring State of Previous User Input after Refresh

查看:138
本文介绍了防止Firefox在刷新之后恢复以前用户输入的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个复选框的页面,需要在数据库中表示状态。复选框是AJAX驱动的,所以当选中一个时,会发送一个请求来更新数据库中的状态。如果他们的观点陈旧,受影响/相关复选框的视图被更新,并且用户被通知,并且必须再次尝试。当我刷新时,我希望复选框表示从数据库中的数据生成的HTML中实际返回的内容, not 与输入元素之间的任何以前的用户交互。



我如何做到这一点,所以Firefox不会更新我已经与之交互的复选框,所以它们与新近收到的HTML中的状态是一样的?



问题很混乱,因为:


  1. 当我在Firefox中刷新页面时,复选框在新收到的HTML中更新,Firefox会更新视图,就像我期望的那样。

  2. 然而,如果我与复选框交互,然后刷新,THAT复选框不会更新每个新收到的HTML。


无论是 checked =checked还是checked属性都不存在,Firerfox都会这样保持,除非我进行shift-refresh。



我已经添加了 window.onunload = function(){}; 因为我认为这可能是由于bfcache造成的,但它不是。

试着用下面的标题,它仍然恢复状态。

  HTTP / 1.1 200 OK 
日期:2012年3月15日(星期四)21:19:02 GMT
服务器:Apache / 2.2.20(Ubuntu)
过期:0
Pragma:no-cache
Cache-Control:no-cache,no-store,must-revalidate
Vary:Accept-Encoding
Content-Encoding:gzip
Keep -Alive:timeout = 5,max = 100
连接:Keep-Alive
Transfer-Encoding:chunked
Content-Type:text / html; charset = ISO-8859-1

尝试循环遍历元素,尽管源html中不存在checked属性,但JavaScript将checked属性视为被检查,所以那里在Firefox恢复之前输入的值后,没有办法恢复接收到的数据源的值。

反过来也是如此...源代码中说 checked =checked,onload JavaScript认为已检查 undefined

< >

  window.onload = function(){
$ ('input(type)='checkbox']')。
返回false
});

$ / code>


解决方案

document.formname.reset()工作,似乎从原来的HTML恢复。这是我尝试过的唯一方法...



感谢Dennis发表类似问题的答案。火狐:如何重新加载窗体*没有缓存用户输入?



这个解决方案的唯一问题是我不需要一个表单,并且不使用表单或提交表单等。我正在使用这些复选框创建一个用户界面似乎是一个完全有效的用例,但为了这个工作,我需要把我所有的输入与表单关联。


I have a page with several check boxes that needs to represent the state in a database. The check boxes are AJAX driven so when one is checked, a request is sent to update state in the database. If their view was stale the view for affected/related check boxes are updated, and user notified, and must try again. When I refresh I'd like the check boxes to represent what is actually returned in HTML generated from the data in the database, not some arbitrary previous user interactions with the input elements.

How do I make it so Firefox doesn't update these check boxes I've interacted with so they are identical to the state in the newly received HTML?

The problem is confusing because:

  1. When I refresh a page in Firefox and the checked attribute for the check box is updated in the newly received HTML, Firefox will update the view, as I would expect it to.

  2. However, if I interact with the check box, then refresh, THAT check box is not updated per the newly received HTML.

ie. It doesn't matter if checked="checked" or the checked attribute is non-existant, Firerfox keeps it that way unless I do a shift-refresh.

I've even added window.onunload = function(){}; because I thought it might be due to the bfcache, but it is not.

Tried with the following headers and it still restored the state.

HTTP/1.1 200 OK
Date: Thu, 15 Mar 2012 21:19:02 GMT
Server: Apache/2.2.20 (Ubuntu)
Expires: 0
Pragma: no-cache
Cache-Control: no-cache, no-store, must-revalidate
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html;charset=ISO-8859-1

Tried looping over the elements, and despite the checked attribute not existing in the source html, JavaScript considered the checked attribute to be "checked.", so there is no way to restore the value from the received source, after Firefox restores the value from previously entered input.

The converse is also true... the source says checked="checked" and the onload JavaScript considers checked undefined.

Source for alerting on the state of the first checkbox.

window.onload = function() {   
    $('input[type="checkbox"]').each(function() {
        alert($(this).attr('checked'));
        return false
    });
}

解决方案

document.formname.reset() worked, which seems to restore from whatever the HTML was originally. It's the only method I tried that works...

Thanks to Dennis for posting this answer for a similar question. Firefox: How to reload form *without* caching user-input?

The only problem with this solution is I do not need a form, and am not using a form, or submitting a form, etc. I am using these check boxes to create a user interface which seems to be a perfectly valid use case, but in order for this to work, I needed to associate all my inputs with a form.

这篇关于防止Firefox在刷新之后恢复以前用户输入的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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