如何判断一个窗口是否存在于 Javascript 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4785399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:05:04  来源:igfitidea点击:

How to tell if a window exists in Javascript?

javascript

提问by Andrew

I've seen ways of seeing if a window a particular script opened is still opened, but what if it didn't?

我已经看到了查看特定脚本打开的窗口是否仍然打开的方法,但如果没有呢?

I have a small window that has a button to click to load the large window. When I close the large one, I want a particular onUnloador onBeforeUnloadto fire iff the small one is closed; if it's still open, those procedures will not fire. I may be having simply a massive brain fart but I can't figure out how to check if the other window is open. The big one isn't opening it, so I can't simply record the handle from opening it.

我有一个小窗口,上面有一个按钮可以点击加载大窗口。当我关闭大的时,我想要一个特定的onUnloadonBeforeUnload当小一个关闭时触发;如果它仍然打开,这些程序将不会触发。我可能只是有一个巨大的脑放屁,但我不知道如何检查另一个窗口是否打开。大的没有打开它,所以我不能简单地记录打开它的手柄。

In shorter terms: If window A opened window B, how can I check within window B if window A still exists?

简而言之:如果窗口 A 打开了窗口 B,我如何在窗口 B 中检查窗口 A 是否仍然存在?

回答by chaos

if(window.opener && !window.opener.closed)
    alert('Yup, still there.');

回答by Juan Mendes

window.closedwill be set to true if you popped a window and it was closed (by script or user).

window.closed如果您弹出一个窗口并且它已关闭(由脚本或用户),则将设置为 true。

var win = window.open('...')';
if (win.closed)

Your case seems to be the following:

你的情况似乎如下:

From a popup window, you can check if the window that opened it is still open using window.opener.closed

从弹出窗口中,您可以检查打开它的窗口是否仍然打开使用 window.opener.closed

Get handle to a window by name

按名称获取窗口句柄

I mentioned there's no way to just get the window handle by name in the comments. However, I did some research and found that the following works in FF/IE/Chrome; it's a hack, I didn't see it mentioned anywhere as the expected behavior, so I wouldn't rely on it too much, but it was fun to find it works! In my code, I would still just make sure to pass around the required handles.

我提到没有办法在评论中按名称获取窗口句柄。但是,我做了一些研究,发现以下在 FF/IE/Chrome 中有效;这是一个 hack,我没有看到任何地方提到它作为预期的行为,所以我不会太依赖它,但发现它有效很有趣!在我的代码中,我仍然会确保传递所需的句柄。

//opened a window without storing a handle, but gave it a name
window.open('/some/url', 'xxx');

// now I need to get a reference to that window
// Calling open without setting a url gets you
// a reference and doesn't reload the window
var win = window.open('', 'xxx')

回答by Audrius

Use try{} and catch(err){}.

使用 try{} 和 catch(err){}。

try{
window.opener.document.body.getElementById('#elementId'); // do some action with window. opener
}
catch(err){
// if we are here then probably there is no window.opener
window.close(); // closing this window or whatever you want
}

回答by Ritwik

Try the following code:

试试下面的代码:

if (!!window) {
  console.log('Exist');
}