chrome 中的 javascript self.close()

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

javascript self.close() in chrome

javascriptgoogle-chrome

提问by myggul

I want to auto-close window after users confirm password changed, so

我想在用户确认密码更改后自动关闭窗口,所以

<script>
    alert('password changed!');
    window.opener = window.location.href; 
    self.close();
</script>

It works in IE, but not works in Chrome.

它适用于 IE,但不适用于 Chrome。

<script>
    window.open('', '_self', '');
    window.close();
</script>

this not works , too

这也行不通

why not works in Chrome ? & How can i work this?

为什么不能在 Chrome 中工作?& 我该如何工作?

回答by Niet the Dark Absol

Under normal circumstances, JavaScript can only close a window that was opened by JavaScript. While certain "hacks" may exist involving window.openeror "opening" a new window in the current tab, they are unreliable because they are bypassing a security thing.

一般情况下,JavaScript 只能关闭一个由 JavaScript 打开的窗口。虽然某些“黑客”可能存在,涉及window.opener或“打开”当前选项卡中的新窗口,但它们不可靠,因为它们绕过了安全问题

In any case, window.close()should either close the window (if it was opened by JavaScript), or pop up a confirmation asking if the user wants to allow the page to be closed.

在任何情况下,window.close()要么关闭窗口(如果它是由 JavaScript 打开的),要么弹出一个确认询问用户是否允许关闭页面。

回答by Arpit Jain

I know it is very late to answer this question. But just in case, it helps somebody in future.

我知道现在回答这个问题已经很晚了。但以防万一,它可以帮助将来的某个人。

It is not possible to close the self window with the same javascript. It is considered as the security issue. It was a bug in the previous versions of the browsers that it used to work then. But it was fixed later. Now, it is not possible to close the "self" window using Javascript.

无法使用相同的 javascript 关闭自身窗口。它被认为是安全问题。这是以前版本的浏览器中的一个错误,当时它可以正常工作。不过后来修好了。现在,无法使用 Javascript 关闭“自我”窗口。

You can refer to the following stackoverflow answer for more information on this: Answer posted on some other similar question on the stackoverflow.

您可以参考以下 stackoverflow 答案以获取更多信息: 在 stackoverflow 上的其他一些类似问题上发布的答案

Hope it clarifies.

希望它澄清。