从关闭直到返回Ajax响应preventing Web浏览器 [英] Preventing web browser from closing until AJAX response is returned

查看:94
本文介绍了从关闭直到返回Ajax响应preventing Web浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Web浏览器中运行一个游戏(作为一个插件)和我想要做的是:

I've got a game that runs in the web browser (as a plugin) and what I'm trying to do is:


  1. 检测用户是否决定关闭浏览器(ALT + F4,击中X按钮等)

  1. Detect if the user has decided to close the browser (Alt+F4, hitting the 'X' button etc)

prevent的收盘从我们同时要解雇我们的Web服务的调用来记录用户关闭浏览器浏览器

Prevent the browser from closing whilst we fire a call to our web services to log that the user has closed the browser

一旦我们收到来自Web服务的响应,解除锁定,并允许浏览器关闭的要求。

Once we receive the response from the web services, release the lock and allow the browser to close as requested.

我们想这样做的主要原因是,我们有一些并发问题,并要通过我们的日志,我们要隔离的人注销/关闭从哪里插件崩溃真正的实例浏览器。

The main reason we want to do this is we're having some concurrency problems and going through our logs we want to isolate people logging out / closing the browser from genuine instances where the plugin has crashed.

我看着使用jQuery做这个(用于X浏览器兼容性 - Opera将无法正常工作,但我们不会对任何歌剧反正用户谢天谢地):

I looked into doing this with JQuery (for X-Browser compatability - Opera won't work but we don't have any users on Opera anyway thankfully):

$(window).bind('beforeunload', function(e) {
    e.preventDefault();
    // make AJAX call
});

问题是,这显示一个确认对话框被发送AJAX调用之前,用户可以确认用户(您确定要离开这个页面')

The problem is that this displays a confirmation dialog to the user ('Are you sure you want to leave this page') which the user might confirm before the AJAX call is sent.

所以现在的问题是,是否有$ P $的pventing的关闭,直到接收到响应的浏览器的方法吗?当网页被更改,以及还beforeunload火 - 是有区别的点击一个链接的方式与实际点击关闭

So the question is, is there a way of preventing the browser from closing until the response is received? Also 'beforeunload' fires when the page is changed as well - is there a way of distinguishing clicking on a link from actually clicking close?

感谢您的帮助WRT这个!

Grateful for any help wrt to this!

推荐答案

其棘手的业务,以避免beeing关闭浏览器窗口。其实,有没有办法做到这一点,返回非旁 - onbeforeunload 事件未定义值,就像你说明。

Its tricky business to avoid the browser window from beeing closed. Actually, there is no way to do that, beside returning a non-undefined value from the onbeforeunload event, like you described.

有一个可能的建议我可以做,那就是创建一个同步的AJAX的 onbeforeunload 事件中请求。例如

There is one possible suggestion I can make, that is creating a synchronized ajax request within the onbeforeunload event. For instance

window.onbeforeunload = function() {
    $.ajax({
        url: '/foo',
        type: 'GET',
        async: false,
        timeout: 4000
    });
};

在理论上,这将阻止为最多4秒的浏览器。在现实中,浏览器将区别对待这一点。例如,火狐(我测试9),的确会不会立即关闭该窗口,但它也不会尊重那里的超时值。我想有像会有2秒内最大的请求被取消和窗口/标签被关闭之前。然而,这应足以在大多数情况下,我猜。

In theory, this will block the browser for a maximum of 4 seconds. In reality, browsers will treat this differently. For instance, Firefox (I tested it on 9), will indeed not close the window immediately, but it also does not respect the timeout value there. I guess there is an internal maximum of like 2 seconds before the request is canceled and the window/tab gets closed. However, that should be enough in most cases I guess.

您的其他问题(如何点击一个链接区分),是相当简单的。如上所述, onbeforeunload 的外观正从它的事件处理程序返回。所以,让我们假设我们有一个变量,它的全球的为我们的应用程序,我们可以做这样的事情。

Your other question (how to distinguish between clicking a link), is fairly simple. As described above, onbeforeunload looks what is getting returned from its event handlers. So lets assume we have a variable which is global for our application, we could do something like

var globalIndicator = true;

// ... lots of code

window.onbeforeunload = function() {
    return globalIndicator;
};

在这一点上,我们总是收到一个确认对话框当窗口/选项卡即将获得关闭。如果我们想避免任何锚点击,我们可以修补它像

At this point, we would always receive a confirmation dialog when the window/tab is about to get closed. If we want to avoid that for any anchor-click, we could patch it like

$( 'a[href^=http]' ).on('click', function() {
    globalIndicator = undefined;
});

这篇关于从关闭直到返回Ajax响应preventing Web浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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