gpt4 book ai didi

javascript - onbeforeunload 处理程序真的会停止我的页面缓存吗?

转载 作者:行者123 更新时间:2023-12-01 15:18:40 29 4
gpt4 key购买 nike

2007 年,在一篇名为“Mastering The Back Button With Javascript”的帖子中,Patrick Hunlock 声称只包含一个 onbeforeunload页面中的处理程序将停止缓存。 (我猜是浏览器缓存。)他对此非常强调:

Just having an unbeforeunload event handler -- regardless of whether or not it actually does anything, regardless of whether or not you spawn a dialog box or not, even if the entire function declaration consists entirely of just { } -- just defining an event handler will prevent the page from being cached -- ever.

As a matter of fact, even if you allow page caching, the page will be not be cached. Having an onbeforeunload event means the page will be re-built every single time it is accessed. Javascripts will re-run, server-side scripts will be re-run, the page will be built as if the user was hitting it for the very first time, even if the user got to the page just by hitting the back or forward button.



有趣的是,除了提到 Hunlock 帖子的一两个人之外,我在其他任何地方都找不到任何提及。任何人都可以澄清这个问题吗?这是真的吗,还是我可以安全地使用该事件?

最佳答案

对于考古学家来说,这被记录为一个特征 introduced in Firefox 1.5 .

注意不要将缓存与浏览器历史记录(也称为存储)混淆 - 页面元素可能包含在两者中。如果期望的行为是强制浏览器在使用前进和后退导航按钮的情况下从服务器重新加载 HTML 页面(例如,因为页面显示敏感信息,或者使用 Javascript 或 Ajax 调用更改了内容),那么Cache-control no-store应该使用 - 或者更好地同时使用 no-storeno-cache .

进一步阅读:

  • Why both no-cache and no-store should be used in HTTP response?
  • OWASP: Testing for Browser cache weakness
  • 关于javascript - onbeforeunload 处理程序真的会停止我的页面缓存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24145548/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com