如何检查用户离开页面

如何检查用户离开页面

本文介绍了如何检查用户离开页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道用户离开网页。我认为可能有三种情况:1。移动到同一网站的另一个页面,打开一个新窗口或将新页面加载到当前窗口,2。移动到不同站点的页面,然后3.关闭窗口。

I'd like to know that a user leave from a Web page. I think there might be three scenarios: 1. move to another page in the same Web site, either open a new window or load a new page to the current window, 2. move to a page in different site, and 3. close the window.

根据这些情况,我在想的是检查当前窗口是否是焦点。所以我的第一个问题是这是正确的方法吗?第二个问题是我应该如何在javascript中实现它?

Based on these scenarios, what I'm thinking is to check whether the current window is the focus. So my first question is Is this the right way? And the second question is How should I implement it in javascript?

推荐答案

看看(原理相同)。有两个相关事件:卸载和beforeunload。另请参阅。

Take a look Mastering The Back Button With Javascript (it's the same principle). There are two relevant events: unload and beforeunload. Also see Best way to detect when user leaves a web page.

这篇关于如何检查用户离开页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:56