Html 浏览器刷新行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:13:27  来源:igfitidea点击:

Browser refresh behaviour

htmlhttprefresh

提问by Marcus Downing

When a user hits Refresh on their browser, it reloads the page but keeps the contents of form fields. While I can see this being a useful default, it can be annoying on some dynamic pages, leading to a broken user experience.

当用户在浏览器上点击刷新时,它会重新加载页面但保留表单字段的内容。虽然我认为这是一个有用的默认设置,但它在某些动态页面上可能会很烦人,从而导致用户体验不佳。

Is there a way, in HTTP headers or equivalents, to change this behaviour?

有没有办法在 HTTP 标头或等价物中改变这种行为?

回答by Joseph Bui

<input autocomplete="off">

回答by Espo

This should do the trick:

这应该可以解决问题:

<body onload="document.FormName.reset();">

Replace FormName with the name of your form and then all the fields will be reset when the user clicks refresh in his browser.

将 FormName 替换为您的表单名称,然后当用户在其浏览器中单击刷新时,所有字段都将被重置。

Or if you only want to reset some fields, add this to the bottom of your page:

或者,如果您只想重置某些字段,请将其添加到页面底部:

<script type="text/javascript">
    document.getElementById('field1').value ='';
    document.getElementById('field2').value ='';
    document.getElementById('field3').value ='';
</script>

That will reset the fields every time a user enters the page, including refreshes

每次用户进入页面时都会重置字段,包括刷新

回答by Fabien

Add the autocomplete attribute set to "off" to the inputs you don't want to be refreshed. For instance:

将设置为“关闭”的自动完成属性添加到您不想刷新的输入。例如:

<input type="text" name="pin" autocomplete="off" />

see the W3C reference

请参阅 W3C 参考

although not mentioned in the reference, it works also with checkboxes, at least on firefox.

尽管参考文献中没有提到,但它也适用于复选框,至少在 Firefox 上是这样。

回答by Edward Wilde

You could call the reset() method of the forms object from the body load event of your html document to clear the forms.

您可以从 html 文档的正文加载事件调用表单对象的 reset() 方法来清除表单。

h1. References

H1. 参考

回答by Rob Rolnick

I wonder, if you set the page not to be cached through meta tags, will that fix the problem? http://lists.evolt.org/archive/Week-of-Mon-20030106/131984.htmlIf it does, it'll have the benefit of working on browser's with Javascript disabled.

我想知道,如果您将页面设置为不通过元标记进行缓存,是否可以解决问题?http://lists.evolt.org/archive/Week-of-Mon-20030106/131984.html如果是这样,它将具有在禁用 Javascript 的情况下在浏览器上工作的好处。

回答by ThoriumBR

The data on forms are not part of w3c specification. It's a browser feature to make your life easy. So, if you don't want to keep the data after reloads, you can set all form's values to null after loading it, as Espo said. Even if the page is not cached, it will display the data on the form, because the data aren't part of the page's html code. You can try this too (don't know if it will work):

表单上的数据不是 w3c 规范的一部分。这是一项浏览器功能,可让您的生活更轻松。因此,如果您不想在重新加载后保留数据,则可以在加载后将所有表单的值设置为 null,如 Espo 所说。即使页面没有被缓存,它也会在表单上显示数据,因为数据不是页面 html 代码的一部分。你也可以试试这个(不知道行不行):

<input type="text" name="foo" value="">