简体   繁体   English

打开和关闭JavaScript窗口

[英]open and close javascript window

I want to close a browser window whether it is in open . 我想关闭浏览器窗口,无论它是否处于open Am new for javascript so help me to create the below logic using javascript. 这是javascript的新功能,因此请帮助我使用javascript创建以下逻辑。 My JavaScript code is here: 我的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'
                         );
}

Yes I agree with Christoph 是的,我同意Christoph

if (!myWindow.closed) {
    myWindow.close();
} 

further more 更进一步

Note: there is browser-specific differences with the above. 注意:上述内容与浏览器特定。 If you opened the window with Javascript (via window.open()) then you are allowed to close the window with javascript if you have reference in your case you have that. 如果您使用Javascript (via window.open())打开了窗口,则可以使用javascript关闭该窗口(如果您有引用的话)。 Firefox disallows you from closing other windows. Firefox不允许您关闭其他窗口。 I believe IE will ask the user for confirmation. 我相信IE会要求用户确认。 Other browsers may vary. 其他浏览器可能会有所不同。

You are close to the right result. 您接近正确的结果。

Just check for 只是检查

(myWindow.closed === false) // same as (!myWindow.closed)

instead of 代替

(myWindow.open() == true)

(since open() will just open another window and your check will fail.) (因为open()只会打开另一个窗口,您的检查将失败。)

Also note, that you need to write index.html or http://sub.yourdomain.tld/index.html , but not http://index.html . 还要注意,您需要编写index.htmlhttp://sub.yourdomain.tld/index.html ,而不是http://index.html

Use closed property to check the window status, but first check whether the window object has been created or not. 使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM