在提示onBeforeUnload后检测用户是否停留 [英] Detecting whether user stayed after prompting onBeforeUnload

查看:108
本文介绍了在提示onBeforeUnload后检测用户是否停留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在处理的网络应用程序中,我正在捕获onBeforeUnload以询问用户真的是否想要退出。

In a web app I'm working on, I'm capturing onBeforeUnload to ask the user whether he really wants to exit.

现在,如果他决定留下来,我会做很多事情。
我想弄清楚的是他实际上选择留下来。

Now, if he decides to stay, there are a number of things I'd like to do. What I'm trying to figure out is that he actually chose to stay.

我当然可以声明SetTimeout为x秒,如果那会激发,那就意味着用户仍然在那里(因为我们没有被卸载)。问题是用户可以花时间决定是否留下来...

I can of course declare a SetTimeout for "x" seconds, and if that fires, then it would mean the user is still there (because we didn't get unloaded). The problem is that the user can take any time to decide whether to stay or not...

我首先希望在对话框显示时,SetTimeout调用不会火,所以我可以设置一个短暂的超时,只有当用户选择留下时才会触发。但是,在显示对话框时会超时执行,因此无效。

I was first hoping that while the dialog was showing, SetTimeout calls would not fire, so I could set a timeout for a short time and it'd only fire if the user chose to stay. However, timeouts do fire while the dialog is shown, so that doesn't work.

我尝试的另一个想法是在窗口捕获mouseMoves /文献。当显示对话框时,mouseMoves确实不会触发,除了一个真正适用于我的情况的奇怪异常,因此也不会起作用。

Another idea I tried is capturing mouseMoves on the window/document. While the dialog is shown, mouseMoves indeed don't fire, except for one weird exception that really applies to my case, so that won't work either.

任何人都可以想到其他方法吗?

Can anyone think of other way to do this?

谢谢!

(如果您好奇,捕获mouseMove不起作用的原因是我的页面中有一个IFrame,包含来自另一个域的网站。如果在卸载页面时,焦点位于IFrame内,当对话框显示时,当鼠标从IFrame内部移动到外部时(至少在Firefox中),我得到MouseMove事件触发ONCE。这可能是一个bug,但是,很可能在我们的情况下会发生,所以我不能使用这种方法。)

(In case you're curious, the reason capturing mouseMove doesn't work is that I have an IFrame in my page, containing a site from another domain. If at the time of unloading the page, the focus is within the IFrame, while the dialog shows, then I get the MouseMove event firing ONCE when the mouse moves from inside the IFrame to the outside (at least in Firefox). That's probably a bug, but still, it's very likely that'll happen in our case, so I can't use this method).

推荐答案

我最终做的是挂钩我的文档的click事件,一旦我收到点击,我认为用户已经停留。

What I ended up doing was hooking into the click event for my document, and once I received a click I considered the user had stayed.

在大多数情况下,这不是一个好的解决方案(如果你是DiggBar)你会有很多假阴性(人们会留下来,你永远不会知道它),但在我们的情况下,它是完全合理的,因为人们与我们的网站大量互动,而不仅仅是框架网站。

It's not a good solution, in most cases (if you are a DiggBar) you'll have lots of false negatives (people will have stayed and you'll never know it), but in our case it made perfect sense, because people interact heavily with our site, not only with the framed sites.

基本上,我的代码执行此操作...

Esentially, my code does this...

function OnBeforeUnload() {
    Event.observe(document, "click", UserStayed);
    if (IConsiderTheIFrameMightBeTryingToPopOut) {
        return "The site is trying to escape. Do you want to stay?";
    }
}

function UserStayed() {
Event.stopObserving(document, "click", UserStayed);
    // New we know the user is still with us for sure.
}

这篇关于在提示onBeforeUnload后检测用户是否停留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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