是否有一种方法来检测是否通过刷新,后退按钮或关闭浏览器导致卸载事件? [英] Is there a way in javascript to detect if the unload event is caused via a refresh, the back button, or closing the browser?

查看:80
本文介绍了是否有一种方法来检测是否通过刷新,后退按钮或关闭浏览器导致卸载事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在查看窗口的卸载事件,尝试确定卸载事件是如何触发的,但没有成功。有没有办法确定javascript事件是如何触发的?




  • 页面刷新

  • 返回按钮(或离开页面)

  • 关闭浏览器



本质上我需要执行一些代码只有当浏览器窗口被关闭,不被刷新或导航时。



目的:当客户更新我们的软件,更新将把他们的第一个互联网请求重定向到一个优惠页面。有一个Do not Bother选项的按钮,但有些用户只会关闭浏览器。关闭浏览器后,我需要复制Do not Bother功能,以便用户不再被重定向到报价页面。简单地附加到卸载事件将无法工作,因为离开页面的不同方式。

解决方案

否,如果有没有依赖浏览器。
当用户关闭页面时,您尝试运行什么样的代码?
是注销用户吗?
然后,如果浏览器崩溃或网络连接中断(如果计算机进入睡眠/休眠模式可能不会),用户将不会注销。



如果是注销目的,您应该可以在服务器上使用每个请求进行更新的时间戳变量(或使用ajax-ping),如果在指定的时间内没有看到该用户,则可以注销该用户。 / p>

更新:找到这个答案在stackoverflow。


I am currently looking at the "unload" event of a window to try to determine how the "unload" event was triggered, but am having little success. Is there a way to determine how the javascript event was triggered?

  • Page Refresh
  • Back Button (or navigate away from the page)
  • Closing the Browser

Essentially I need to execute some code only when the browser window is being closed, not refreshed or navigated away from.

Purpose: When a customer does an update of our software, the update will redirect their first Internet request to an offer page. There is a button for a "Do Not Bother" option, but some users will simply close their browser. Upon closing the browser, I need to duplicate the "Do Not Bother" functionality so the user no longer gets redirected to the offer page. Simply attaching to the "unload" event will not work due to the different ways of leaving a page.

解决方案

No, and if there was it would be browser dependent. What kind of code are you trying to run when the user closes the page? Is it to logout the user? Then the user would not be logged out if the browser crashes or the network connection breaks (and probably not if the computer goes to sleep/hibernation mode).

If it is for logout-purposes you should probably use a timestamp variable at the server that gets updated with every request (or use a ajax-ping), and logout the user if it hasn't been seen for a specified time.

Update: Found this answer here at stackoverflow.

这篇关于是否有一种方法来检测是否通过刷新,后退按钮或关闭浏览器导致卸载事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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