window.close and self.close do not close the window in Chrome
来源:互联网 发布:炫踪网络 ceo 编辑:程序博客网 时间:2024/06/05 11:13
The issue is that when I invoke window.close
or self.close
it doesn't close the window. Now there seems to be a belief that in Chrome you can't close by script any window that is not script created. That is patently false but regardless it is supposed to still do it, even if it requires to pop up an alert to confirm. These are not happening.
So does anyone have real, functional and proven method of closing a window using something like javascript:window.close
or javascript:self.close
that actually does what is expected and something that happens just fine in every browser that is NOT Chrome based? Any suggestions would be greatly appreciated and I am looking for Javascript specific solution, nothing JQuery or third party implementation.
Ordinary javascript cannot close windows willy-nilly. This is a security feature, introduced a while ago, to stop various malicious exploits and annoyances.
From the latest working spec for window.close()
:
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.
This means, with one small exception, javascript must not be allowed to close a window that was not opened by that same javascript.
Chrome allows that exception -- which it doesn't apply to userscripts -- however Firefox does not. The Firefox implementation flat out states:
This method is only allowed to be called for windows that were opened by a script using the
window.open
method.
If you try to use window.close
from a Greasemonkey / Tampermonkey / userscript you will get:
Firefox: The error message, "Scripts may not close windows that were not opened by script.
"
Chrome: just silently fails.
The long-term solution:
The best way to deal with this is to make a Chrome extension and/or Firefox add-on instead. These can reliably close the current window.
However, since the security risks, posed by window.close
, are much less for a Greasemonkey/Tampermonkey script; Greasemonkey and Tampermonkey could reasonably provide this functionality in their API (essentially packaging the extension work for you).
Consider making a feature request.
The hacky workarounds:
Chrome is currently vulnerable to the "self redirection" exploit. So code like this will currently work in Tampermonkey scripts:
open(location, '_self').close();
This is buggy behavior, IMO, and is liable to be blocked by future releases of Chrome, so use this hack with that in mind.
Firefox is secure against that exploit. So, the only javascript way is to cripple the security settings, one browser at a time.
You can open up about:config
and setallow_scripts_to_close_windows
to true
.
If your script is for personal use, go ahead and do that. If you ask anyone else to turn that setting on, they would be smart, and justified, to decline with prejudice.
There currently is no equivalent setting for Chrome.
From my observation, this update fixed the issue on using window.close()
to close the popup window. You will see this in the console when it fail, "Scripts may close only the windows that were opened by it.". That means The hacky workarounds (Brock Adams's answer) may not work in the latest release.
So, in the previous Chrome released builds, the below code block may worked but not with this update.
window.open('', '_self', '');window.close();
For this update, you have to update your code accordingly to close the popup window. One of the solution is to grab the popup window id and use
chrome.windows.remove(integer windowId, function callback)
method to remove it. Chrome extension windows API can be found at chrome.windows.
Actually my chrome extension MarkView was facing this issue and I had to update my code to make it work for this Chrome Update. By the way, MarkView is tool to read and write Awesome Markdown Files, it provides features including Content Outline, Sortable Tables and code block syntax highlight with line number.
I also created this post, any comments are welcome.
I use top.close() to close a tab. window.close() or other open...close didn't work for me.- window.close and self.close do not close the window in Chrome
- window.close
- window.close()
- window.close()
- Window.close()
- Close a IE11 window without receiving the “Do you want to close this window” prompt?
- AboutView -- Close the second window
- Why is the window.close() works in IE browser but not FireFox? "Warning: Scripts may not close windo
- Window.Close();Window.Open();
- Window.Close();Window.Open();
- Window.Close();Window.Open();
- Window.Close();Window.Open();
- Window.Close();Window.Open();
- window.close()不起作用?
- 关于Window.close()
- window.open&close
- Open/Close window
- ext window close() 问题
- 我的macbook air只有一个耳机有声音,右边的耳机没有声音,原装耳机也是一样
- 如何修改 SAP 登录界面的文字(事务码SE61)
- HDU 1080 Human Gene Functions
- Jquery中toggle的用法
- 传参出错注意数据库连接问题
- window.close and self.close do not close the window in Chrome
- C++实现24位位图的灰度化
- 『转』android 程序中判断当前是否连接网络,网络是否可用
- 利用.dSYM和.app文件准确定位Crash位置
- Android 命令行手动编译打包详解
- 如何修改 SAP 登录后的背景图片(事务码 SMW0,SM30)
- ireport+jasperreport+jfreechart完成数据分析
- Linux中ctrl+z 、ctrl+c、 ctrl+d区别
- SQL 统计语句