简体   繁体   English

onbeforeunload 事件不会在浏览器按钮点击时触发,而是在 CMD+R/F5 上工作

[英]onbeforeunload event not firing on browser button click, but working on CMD+R/F5

I'm sure I'm missing something relatively simple - but for the life of me I can't find the answer.我确定我错过了一些相对简单的东西 - 但对于我的生活,我找不到答案。 When trying to do a reload prevention in react , my onbeforeunload function is not firing when I press the reload button in the browser menu (Chrome).尝试在react进行重新加载预防时,当我按下浏览器菜单 (Chrome) 中的重新加载按钮时,我的onbeforeunload功能不会触发。 It works if I press CMD+R/F5 and once that's done once - the browser button also fires the function.如果我按 CMD+R/F5 并且一旦完成它就可以工作 - 浏览器按钮也会触发该功能。 It simply doesn't work if I attempt to click reload first.如果我尝试先单击重新加载,它根本不起作用。 Additionally, if I navigate through the router once, it also seems to register.此外,如果我浏览路由器一次,它似乎也注册了。 I am using the following code to register the refresh on my top level template:我正在使用以下代码在我的顶级模板上注册刷新:

class Template extends React.Component {
    constructor(props) {
        super(props);
    }

    refreshPrevent = (e) => {
        e.preventDefault();
        e.returnValue = true;
    }

    componentDidMount() {
        console.log("registering refresh handlers");
        window.addEventListener("beforeunload", this.refreshPrevent );
    }

    componentWillUnmount() {
        window.removeEventListener("beforeunload", this.refreshPrevent);
    }

Any ideas would be appreciated.任何想法,将不胜感激。

I don't see any errors in your code.我在您的代码中没有看到任何错误。 I use similar code in my react applications, but I hadn't tested this particular problem before until you posted.我在我的react应用程序中使用了类似的代码,但在您发布之前我还没有测试过这个特定问题。

It seems, according to the MDN documentation that this behavior is normal: 根据 MDN 文档,这种行为似乎是正常的:

To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with.为了消除不需要的弹出窗口,某些浏览器不会显示在 beforeunload 事件处理程序中创建的提示,除非页面已与该页面交互。 Moreover, some don't display them at all.此外,有些根本不显示它们。

So, when your app first loads, until you interact with the page in some way, you can hit the browser refresh button and depending on the browser, the page will reload without displaying a prompt.因此,当您的应用首次加载时,在您以某种方式与页面交互之前,您可以点击浏览器刷新按钮,并且根据浏览器的不同,页面将重新加载而不显示提示。

However, once you have done something on the page with your mouse or keyboard or via touch, then the prompt will be displayed.但是,一旦您使用鼠标或键盘或通过触摸在页面上完成了某些操作,就会显示提示。

The MDN documentation concludes (emphasis added): MDN 文档总结(强调):

Note also, that various browsers ignore the result of the event and do not ask the user for confirmation at all.还要注意,各种浏览器会忽略事件的结果,根本不要求用户确认。 In such cases, the document will always be unloaded automatically.在这种情况下,文档将始终自动卸载。 Firefox has a switch named dom.disable_beforeunload in about:config to enable this behaviour. Firefox 在 about:config 中有一个名为 dom.disable_beforeunload 的开关来启用此行为。 As of Chrome 60, the confirmation will be skipped if the user has not performed a gesture in the frame or page since it was loaded.从 Chrome 60 开始,如果用户自加载后没有在框架或页面中执行手势,则确认将被跳过。

This fits with the spec that we should expect certain situations, like an uninteracted-with page plus refresh, to skip prompting the user:符合我们应该期望某些情况下的规范,例如未与页面交互加刷新,跳过提示用户:

The user agent is encouraged to avoid asking the user for confirmation if it judges that doing so would be annoying, deceptive, or pointless.如果用户代理认为这样做会很烦人、具有欺骗性或毫无意义,则鼓励用户代理避免要求用户进行确认。 A simple heuristic might be that if the user has not interacted with the document, the user agent would not ask for confirmation before unloading it.一个简单的启发可能是,如果用户没有与文档交互,用户代理在卸载它之前不会要求确认。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CMD + R在chrome:// extensions /中不再起作用了 - CMD+R not working anymore in chrome://extensions/ event.preventDefault不适用于CMD + R刷新 - event.preventDefault does not work for CMD+R refresh 防止在刷新/ F5中发生OnBeforeUnload()事件 - prevent OnBeforeUnload() event from happening in refresh/F5 onbeforeunload上需要帮助或单击浏览器后退按钮 - Help required on onbeforeunload or click on browser back button 为什么 onbeforeunload 事件没有触发 - Why onbeforeunload event is not firing 当在JavaScript中有window.onbeforeunload时,刷新按钮/ F5直接发送到控制器(mvc) - Refresh button/F5 send directly to the controller(mvc) when have window.onbeforeunload in a javascript 按钮单击事件仅在Edge浏览器上触发两次 - Button click event firing twice only on Edge browser 在window.onbeforeunload事件中使用window.event.keyCode在javascript中捕获f5按键事件总是0而不是116 - capturing f5 keypress event in javascript using window.event.keyCode in window.onbeforeunload event is always 0 and not 116 按F5或刷新按钮时强制重新加载浏览器 - Force reload the browser when pressing F5 or refresh button 如何区分刷新(通过 F5/刷新按钮)和关闭浏览器? - How to distinguish between refresh (by F5/refresh button) and close browser?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM