![](/img/trans.png)
[英]Is it posible to open a window then close the window in javascript?
[英]open and close javascript window
我想关闭浏览器窗口,无论它是否处于open
。 这是javascript的新功能,因此请帮助我使用javascript创建以下逻辑。 我的JavaScript代码在这里:
if (myWindow.open() == true) {
myWindow.close();
} else {
myWindow=window.open('http://index.html',
'popUpWindow',
'height=700,width=800,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes'
);
}
是的,我同意Christoph
if (!myWindow.closed) {
myWindow.close();
}
更进一步
注意:上述内容与浏览器特定。 如果您使用Javascript (via window.open())
打开了窗口,则可以使用javascript关闭该窗口(如果您有引用的话)。 Firefox不允许您关闭其他窗口。 我相信IE会要求用户确认。 其他浏览器可能会有所不同。
您接近正确的结果。
只是检查
(myWindow.closed === false) // same as (!myWindow.closed)
代替
(myWindow.open() == true)
(因为open()
只会打开另一个窗口,您的检查将失败。)
还要注意,您需要编写index.html
或http://sub.yourdomain.tld/index.html
,而不是http://index.html
。
使用closed
属性检查窗口状态,但首先检查是否已创建窗口对象。
var myWindow=null;
if (myWindow && !myWindow.closed) { //exist and is not closed
myWindow.close();
myWindow=null; //delete the object
} else { //not exist or is closed
myWindow=window.open('http://index.html',
'popUpWindow',
'height=700,width=800,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes'
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.