从 Chrome V80 开始,很少有像 beforeunload、unload 等事件被弃用。我们如何在 JavaScript 中进一步处理此类现有事件?
我的要求是当用户尝试刷新/重新加载页面(F5 或 ctrl+r)或浏览器后退按钮时提醒,我需要提醒并在确认后,我需要重新加载或后退。这是在 JSF2.2 中
但该行为不应影响其他浏览器中的功能。
谁能帮我解决这个问题
最佳答案
beforeunload
事件仍然可用。正在改变的是在页面关闭期间执行同步 XHR 的能力。从长远来看,真正有害的是同步 XHR,因为它会损害用户体验。您仍然可以使用 beforeunload
。 Getting Rid of Synchronous XHRs特别提到这一点:
Note: We expect to remove support for synchronous use of XMLHTTPRequest() during page unloads in Chrome in version 80, scheduled to ship early in 2020.
在 Chrome Platform Status: Disallow sync XHR in page dismissal 中也有关于此特定更改的信息.在 Deprecations and removals in Chrome 80 中有关于 Chrome 80 的更多信息:
Chrome now disallows synchronous calls to
XMLHTTPRequest()
during page dismissal when the page is being navigated away from or is closed by the user. This applies tobeforeunload
,unload
,pagehide
, andvisibilitychange
.To ensure that data is sent to the server when a page unloads, we recommend
sendBeacon()
orFetch
keep-alive
. For now, enterprise users can use theAllowSyncXHRInPageDismissal
policy flag and developers can use the origin trial flagallow-sync-xhr-in-page-dismissal
to allow synchronous XHR requests during page unload. This is a temporary "opt-out" measure, and we expect to remove this flag in Chrome 82.
请注意,页面关闭期间的弹出窗口也将被禁止:
Pages may no longer use
window.open()
to open a new page during unload. The Chrome popup blocker already prohibited this, but now it is prohibited whether or not the popup blocker is enabled.
关于javascript - 将弃用的 Chrome V80 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59858513/