window.close and self.close do not close the window in Chrome
问题是,当我调用
那么,是否有人使用像
更新:虽然所提出的大部分内容存在严重的局限性和可用性问题,但在脚本标题中使用
普通的javascript无法关闭Windows willy-nilly。这是一个安全功能,不久前介绍,以阻止各种恶意攻击和烦恼。
根据
The
close() method on Window objects should, if all the following conditions are met, close the browsing context A:
- The corresponding browsing context A is script-closable.
- The browsing context of the incumbent script is familiar with the browsing context A.
- The browsing context of the incumbent script is allowed to navigate the browsing context A.
A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a browsing context whose session history contains only one Document.
这意味着,除了一个小例外,不得允许javascript关闭未被同一个javascript打开的窗口。
Chrome允许该异常 - 它不适用于用户脚本 - 但Firefox不支持。 Firefox实施平台说明:
This method is only allowed to be called for windows that were opened by a script using the
window.open method.
如果您尝试使用Greasemonkey / Tampermonkey / userscript中的
Firefox:错误消息"
Chrome:只是默默地失败了。
长期解决方案:
解决此问题的最佳方法是改为使用Chrome扩展程序和/或Firefox插件。这些可以可靠地关闭当前窗口。
但是,由于
考虑发出功能请求。
黑客的解决方法:
Chrome 目前 s>易受"自我重定向"攻击。所以像这样的代码一般用于工作:
1 | open(location, '_self').close(); |
这是有缺陷的行为,IMO,现在(截至2015年4月左右)大部分被阻止。仅当选项卡刚刚打开并且浏览历史记录中没有页面时,它仍然可以从注入的代码开始工作。所以它只在很小的情况下才有用。
但是,变体仍适用于Chrome(v43和v44)以及Tampermonkey(v3.11或更高版本)。使用显式
1 2 3 4 5 6 7 | // ==UserScript== // @name window.close demo // @include http://YOUR_SERVER.COM/YOUR_PATH/* // @grant GM_addStyle // ==/UserScript== setTimeout (window.close, 5000); |
感谢zanetu的更新。请注意,如果只打开一个选项卡,则无法使用此功能。它只关闭其他标签。
Firefox可以抵御这种攻击。因此,唯一的javascript方法是一次性删除一个浏览器的安全设置。
你可以打开
如果您的脚本仅供个人使用,请继续执行此操作。如果你要求其他人改变这种设置,那么他们就会变得聪明,并且有理由拒绝偏见。
目前没有Chrome的等效设置。
Chrome修复了版本36.0.1985.125的安全问题
Chrome 36.0.1985.125星期三,2014年7月16日
发行公告
根据我的观察,此更新解决了使用
因此,在以前的Chrome版本中,以下代码块可能有效,但此更新不起作用。
1 2 | window.open('', '_self', ''); window.close(); |
对于此更新,您必须相应地更新代码以关闭弹出窗口。解决方案之一是获取弹出窗口ID并使用
1 | chrome.windows.remove(integer windowId, function callback) |
删除它的方法。可以在chrome.windows中找到Chrome扩展程序Windows API。
实际上,我的Chrome扩展程序MarkView面临此问题,我必须更新代码以使其适用于此Chrome更新。顺便说一句,MarkView是读取和写入Awesome Markdown文件的工具,它提供的功能包括内容大纲,可排序表和带行号的代码块语法高亮。
我也创建了此帖子,欢迎任何评论。
现在你可以使用tampermonkey
1 | // @grant window.close |
然后只需打电话
1 | window.close(); |
我正在使用Brock Adams发布的方法,它甚至适用于Firefox,如果它是用户启动的话。
1 | open(location, '_self').close(); |
我是通过按下按钮调用它,因此它是用户启动的,并且使用Chrome 35-40,Internet Explorer 11,Safari 7-8和ALSO Firefox 29-35仍然可以正常工作。我使用Windows 8.1和Mac OS X 10.6,10.9和10.10的版本进行了测试,如果不同的话。
完整的代码:
HTML:
1 | <input type="button" name="Quit" id="Quit" value="Quit" onclick="return quitBox('quit');" /> |
JavaScript的:
1 2 3 4 5 6 7 8 | function quitBox(cmd) { if (cmd=='quit') { open(location, '_self').close(); } return false; } |
试试这个测试页面:(现在在Chrome 40和Firefox 35中测试过)
http://browserstrangeness.bitbucket.io/window_close_tester.htm
许多人仍在尝试找到一种使用javascript关闭Chrome浏览器的方法。仅当您将Chrome用作APP启动器时,以下方法才有效-例如信息亭!
我已经测试了以下内容:
我正在使用以下扩展程序:关闭信息亭
我正在按照使用说明进行操作,并且看起来工作正常(请确保在执行测试时清除缓存)。我使用的javascript是(附加到click事件):
1 | window.location.href = '/closekiosk'; |
我希望这对某人有帮助,因为这是我找到的唯一可行的解??决方案。
注意:该扩展程序似乎在后台运行,并添加了Chrome任务栏图标。它选中了以下选项:"让Chrome在后台运行"(或类似的文本)。您可能需要尝试一下,直到它适合您。我取消选中它,现在可以正常使用了!
尽管认为它"显然是假的",但你所说的"似乎是一种信念"实际上是正确的。 window.close的Mozilla文档说
This method is only allowed to be called for windows that were opened by a script using the window.open method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script
你说它"应该仍然这样做",但我认为你不会找到任何支持它的参考,也许你错误地记得了什么?
我找到了一种适合我的新方法
1 2 | var win = window.open("about:blank","_self"); win.close(); |
下面的代码为我工作-
1 2 | window.open('location', '_self', ''); window.close(); |
在Chrome 43.0.2357.81上测试
这可能很旧,但让我们回答一下。
我使用top.close()关闭标签页。 window.close()或其他打开...关闭对我不起作用。
就我而言,该页面需要关闭,但可能已通过链接打开,因此
我选择的解决方案是发出
这样,如果
1 2 | window.close(); window.setTimeout(function(){location.href = '/some-page.php';},1000); |
只有使用window.open()打开新窗口,才能使用如上所述的代码关闭新窗口。这对我来说非常合适:)注意:切勿使用href在新标签页中打开页面。 Window.close()不适用于" href"。请改用window.open()。
试试这样的onclick =" return self.close()"