javascript 使用 window.open 后,window.close 在 Firefox 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11823569/
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-10-26 14:27:44  来源:igfitidea点击:

window.close not working in firefox, after using window.open

javascriptfirefox

提问by sugarball

I'm trying to close the present window by clicking a button. I know window.close() could only work when the window is owned by javascript.

我试图通过单击一个按钮来关闭当前窗口。我知道 window.close() 只能在窗口由 javascript 拥有时才能工作。



So I use window.open()before, then close that window like:

所以我window.open()之前使用,然后关闭该窗口,如:

self.name = "close";
window.open("","close");
self.opener = null;
self.close();`


This works in Chrome and in older versions of Firefox, but not in FF14. So how can I close a window in FF14? thanks!

这适用于 Chrome 和旧版本的 Firefox,但不适用于 FF14。那么如何在 FF14 中关闭窗口呢?谢谢!

I run the following javascript in FF console. It works in IE and Chrome.

我在 FF 控制台中运行以下 javascript。它适用于 IE 和 Chrome。

win=window.open('','_self');
win.close();------undefined

回答by Sunil Rai

Above mentioned comment is right window.close() will work only if the window is opened using window.open().

上面提到的注释是正确的 window.close() 只有在使用 window.open() 打开窗口时才会起作用。

Try this code:

试试这个代码:

<a href="#" onclick="javascript:var win = window.open('', '_self');win.close();return false;">Close</a>

This is working for me in IE, Chrome and Firefox ( even for FF14). HTH

这在 IE、Chrome 和 Firefox 中对我有用(甚至适用于 FF14)。HTH