仅当窗口关闭时才能启动(包括工作示例) [英] Fire onbeforeunload only when window is closed (includes working example)

查看:121
本文介绍了仅当窗口关闭时才能启动(包括工作示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有当标签页或浏览器关闭,而不是刷新,链接点击等时,我只想启动onbeforeunload事件。我发现一个网站有效: https://checkout.deindeal.ch/



在Chrome中触发事件的步骤:


  1. 将某物放入购物车

  2. 点击Zur Kasse

  3. 关闭标签

  4. onbeforeunload显示消息并弹出一个弹出窗口。 onbeforeunload事件,如刷新不会触发。我调试了他们的代码,但找不到他们排除这些事件的地方。

任何人都可以在代码中找到正确的地方?



谢谢!

解决方案

对于想要使用 onbeforeunload 的读者, / p>

事件 onbeforeunload 应用于防止用户的数据丢失,如填写表单和其他数据,不保存,并将在刷新页面时丢失。



使用 onbeforeunload 来防止一个用户离开你的页面是令人讨厌的,并且不会阻止用户离开。






除非用户使用按键可以检测到的快捷方式(例如 Ctrl + F5,否则无法检测到 浏览器选项卡刷新按钮 )。
更多信息:
JavaScript beforeunload检测刷新与关闭






旧答案已被删除,以免误导任何人(现在浏览器无效) 。



有关 onbeforeunload的更多堆栈溢出链接使用:



我如何在javascript onbeforeunload事件中获取目标网址?



如何防止在页面刷新时调用onbeforeunload



Fire onbeforeunload确认e仅限外部站点


I want to fire the onbeforeunload event only when the tab or browser is closed and not upon refresh, link click and so on. I found a site where that works: https://checkout.deindeal.ch/

Steps to fire the event in Chrome:

  1. Put something into the cart
  2. Click on "Zur Kasse"
  3. Close tab
  4. onbeforeunload Message is shown and a popup opens. onbeforeunload events like refresh will not fire. I debugged their code, but could not find the place where they exclude those events or whatsoever.

Anyone out there who can find the correct place in the code?

Thanks!

解决方案

For readers that want to use onbeforeunload,

The event onbeforeunload should be used for preventing user's data loss, like filled in forms and other data that are not saved and will be lost upon page refresh.

Using onbeforeunload to prevent a user from leaving your page is annoying and will not stop the user from leaving.


You can not detect a browser tab refresh button press, unless the user use a shortcut that you can detect with key press (like Ctrl+F5). More info here : javascript beforeunload detect refresh versus close


Old answer was removed in order not to mislead anyone (it was not working on nowadays browsers).

More stack overflow links related on onbeforeunload use:

How can i get the destination url in javascript onbeforeunload event?

How to prevent calling onbeforeunload when page refresh

Fire onbeforeunload confirm alert for external sites only

这篇关于仅当窗口关闭时才能启动(包括工作示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆