Javascript window.close() 不起作用 - 脚本可能只关闭由它打开的窗口

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

window.close() doesn't work - Scripts may close only the windows that were opened by it

javascriptwindowkeyboard-shortcutsshortcut

提问by mayconfsbrito

I'm having a problem always when I'm trying to close a window through the window.close()method of the Javascript, while the browser displays the below message on the console:

当我尝试通过window.close()Javascript的方法关闭窗口时,我总是遇到问题,而浏览器在控制台上显示以下消息:

"Scripts may close only the windows that were opened by it."

This occurs in every part of the page. I can run this directly of a link, button, or a script, but this message always are displayed.

这发生在页面的每个部分。我可以直接通过链接、按钮或脚本运行它,但始终会显示此消息。

I'm tried to replace the window.close();method for the functions (or variants of these) below, but nothing happened again:

我试图替换window.close();下面的函数(或这些函数的变体)的方法,但没有再发生任何事情:

window.open('', '_self', '');
window.close();

采纳答案by mayconfsbrito

I searched for many pages of the web through of the Google and here on the Stack Overflow, but nothing suggested resolved my problem.

我通过 Google 和 Stack Overflow 上搜索了许多网页,但没有任何建议可以解决我的问题。

After many attempts and "kicks", I changed my way of to test that controller. Then I have discovered that the problem occurs always which I reopened the page through of the Ctrl + Shift + Tshortcut in Chrome. So the page ran, but without a parent window reference, and because this can't be closed.

经过多次尝试和“踢”之后,我改变了测试该控制器的方式。然后我发现问题总是发生在我通过Ctrl + Shift + TChrome中的快捷方式重新打开页面时。所以页面运行了,但没有父窗口引用,因为它无法关闭。

回答by somethinghere

Error messages don't get any clearer than this:

错误消息没有比这更清楚:

"Scripts may close only the windows that were opened by it."

If your script did not initiate opening the window (with something like window.open), then the script in that window is not allowed to close it. Its a security to prevent a website taking control of your browser and closing windows.

如果您的脚本没有启动打开窗口(使用 window.open 之类的东西),则不允许该窗口中的脚本关闭它。它是一种防止网站控制您的浏览器并关闭窗口的安全措施。

回答by andrex

You can't close a current window or any window or page that is opened using '_self' But you can do this

您无法关闭当前窗口或使用“_self”打开的任何窗口或页面,但您可以这样做

var customWindow = window.open('', '_blank', '');
    customWindow.close();

回答by DfrDkn

The below code worked for me :)

下面的代码对我有用:)

window.open('your current page URL', '_self', '');
window.close();

This is not working. Some websites are working fine. But for youtube.com and yahoo.com sites and the like, the above application code is not working. Please suggest any ideas.

这是行不通的。一些网站运行良好。但是对于 youtube.com 和 yahoo.com 等网站,上述应用程序代码不起作用。请提出任何想法。