绕过 Firefox 的刷新行为
如何使用Javascript捕获Firefox浏览器的refresh按钮或事件并模仿IE刷新表单时的行为? Firefox 重新填写表单,这破坏了我的 ajax UI。
How do you capture refresh button or event of Firefox browser using Javascript and imitate the behavior of IE when refreshing forms? Firefox refills the forms which destroys my ajax UI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定 IE 的行为方式,但如果您只想在 ajax UI 工作之前使表单为空,则可以使用
window.beforeunload
在重新加载时清除表单。I'm not sure how IE behaves, but if you just want the form to be empty before your ajax UI does its work, you can clear the form as it reloads by using
window.beforeunload
.https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion
https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion
您可以跟踪输入的所有内容(onchange 等)、编码并将其放入本地存储中。加载页面时,查看本地存储中是否有任何内容,并使用用户的数据填写表单。
确保在需要时清空存储(表单提交、卸载前、读取数据后等)。
如果您要解决的问题如此狭窄,您也可以使数据在存储中的寿命非常短暂。另外,如果可以的话,将其设置为仅会话。您还可以将数据保存限制为 onbeforeunload 事件,而不是在每次更改时保存。至少,在 onchange 触发后,在合理的秒数内执行 setTimeOut() 并在 setTimeOut 处理程序中进行保存,这样就不会不必要地耗尽 CPU。
You can keep track of everything entered (onchange, etc), encode, and put it into local storage. When the page loads, see if you have anything in local storage and fill the form with the user's data.
Make sure you empty storage when needed (form submittal, onbeforeunload, after reading the data, etc.)
If the problem you're trying to solve is so narrow, you can make the data very short-lived in storage too. Also, make it session-only if you can. You can also limit the saving of data to the onbeforeunload event, instead of saving on every change. At the very least, do a setTimeOut() with some reasonable amount of seconds after onchange is fired and do your saving in the setTimeOut handler, this way you won't use up CPU unnecessarily.