窗户能够关闭吗? [英] Is Window Close-able?

查看:187
本文介绍了窗户能够关闭吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道 window.close()是否真的要关闭窗口。这不是关闭窗口 - 如何确定窗口的重复是开放的?因为该解决方案依赖于检查 window.name ,这并非完全万无一失;弹出窗口可以用不同的名字打开,但仍然可以通过 window.close()来接近。

I need to know whether window.close() is actually going to close the window. This is not a duplicate of Close window - How to determine how window was opened? because that solution relies on checking window.name, which is not exactly foolproof; the popup could be opened with a different name but still be close-able via window.close().

我不能只检查 window.opener 是否已定义,因为在Firefox和Chrome中,如果用户关闭了开启者, window.opener 设置为 null 。例如:

I can't just check whether window.opener is defined or not null because in Firefox and Chrome, if the user closes the opener, window.opener gets set to null. For example:

// In parent window:
window.open();

// In child window:
console.log(window.opener); // outputs a Window object

// Now, click the X on the parent window.

// Continue below in child window:
console.log(window.opener); // outputs null
// However, you can still close the window!
window.close(); // closes the child window

另一方面,如果用户加载了包含此代码的页面新标签:

On the other hand, if the user loaded a page with this code in a new tab:

console.log(window.opener); // also outputs null
window.close(); // doesn't work; window remains open

Firefox然后在错误控制台中抱怨脚本无法关闭未打开的窗口通过脚本,Chrome不做任何事。

Firefox then complains in the Error Console about a script not being able to close windows not opened by scripts, and Chrome does nothing.

我需要检查 window.close()的原因是什么关闭窗口是如果窗口保持打开状态,我想去另一个地址。我想这样做:

The reason that I need to check whether window.close() will close the window is that if the window remains open, I want to go to another address. I thought of doing it this way:

// Try to close window.
window.close();
// If the window is still open after three seconds, go to another page.
setTimeout(function(){
    // For testing purposes:
    alert("Not closed!");
    // What I want to do:
    window.location = "http://www.example.com/";
}, 3000);

然而,三秒钟的滞后会使我的应用程序对用户来说似乎很慢。那不行。在我的电脑上,延迟1毫秒足以让窗户关闭;只有窗口保持打开时才会发出警报。但是,我需要有人确认这对所有计算机都是如此。这也不起作用:

However, a three-second lag would make my application seem slow to the user. That would not do. On my computer, a delay of 1 millisecond was sufficient to let the window close; the alert only happened if the window remained open. However, I would need someone to confirm that this is true for all computers. This also does not work:

try{
    // This will not throw an error regardless of
    // whether the window was actually closed:
    window.close();
}catch(e){
    // This never happens:
    console.log("Could not close window");
}

简而言之,我只需要在JavaScript之前知道,或者在在调用 window.close()之后,窗口是否会实际关闭。我该怎么做?

In short, I just need a way in JavaScript to know, either before or after calling window.close(), whether the window will actually close. How do I do that?

推荐答案

只需致电 window.close()然后检查 window.closed 以查看它是否已关闭。这也将捕获你尝试 close()一个带有beforeunload处理程序的页面并且用户选择不让它关闭的情况....

Just call window.close() and then check window.closed to see whether it closed. That will also catch cases where you try to close() a page with a beforeunload handler and the user elects to not let it close....

哦,并且每个规格 window.close()同步更新 window.closed 如果窗口将要关闭之前返回,那么你不必担心超时等等。

Oh, and per spec window.close() updates window.closed synchronously before returning if the window is going to close, so you don't have to worry about timeouts and whatnot.

这篇关于窗户能够关闭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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