由于在脚本中使用了 window.close(), 当前非弹出窗口在最新版本的chrome和firefox里总是不能关闭,而在 IE中是可以关闭的 。

在console中弹出提示"Scripts may close only the windows that were opened by it" (脚本只能关闭它所打开的窗口),[如下图所示] , 不明白是什么原因。

经过一段时间的折腾。终于明白了问题所在。

首先,什么是非弹出窗口呢?

非弹出窗口,即是指(opener=null 及 非window.open()打开的窗口,比如URL直接输入的浏览器窗体, 或由其它程序调用产生的浏览器窗口)。

 

其次,window.close() 怎么理解呢?

 

由 https://developer.mozilla.org/en-US/docs/Web/API/window.close可知:

Closes the current window, or the window on which it was called.
When this method is called, the referenced window is closed.

This method is only allowed to be called for windows that were opened by a script using thewindow.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.

Examples

Closing a window opened with window.open()

This example demonstrates how to use this method to close a window opened by script callingwindow.open().

<script type="text/javascript">
//Global var to store a reference to the opened window
var openedWindow; function openWindow()
{
openedWindow = window.open('moreinfo.htm');
} function closeOpenedWindow()
{
openedWindow.close();
}
</script>
 

Closing the current window

When you call the window object's close() method directly, rather than calling close() on a windowinstance, the browser will close the frontmost window, whether your script created that window or not.

<script type="text/javascript">
function closeCurrentWindow()
{
window.close();
}
</script>

在某些实际应用中,window.close() and self.close() 是不能关闭非弹出窗口(opener=null及非window.open()打开的窗口)。

方案:

1.

function closeWindows() {
var browserName = navigator.appName;
var browserVer = parseInt(navigator.appVersion);
//alert(browserName + " : "+browserVer); //document.getElementById("flashContent").innerHTML = "<br>&nbsp;<font face='Arial' color='blue' size='2'><b> You have been logged out of the Game. Please Close Your Browser Window.</b></font>"; if(browserName == "Microsoft Internet Explorer"){
var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
if (ie7)
{
//This method is required to close a window without any prompt for IE7 & greater versions.
window.open('','_parent','');
window.close();
}
else
{
//This method is required to close a window without any prompt for IE6
this.focus();
self.opener = this;
self.close();
}
}else{
//For NON-IE Browsers except Firefox which doesnt support Auto Close
try{
this.focus();
self.opener = this;
self.close();
}
catch(e){ } try{
window.open('','_self','');
window.close();
}
catch(e){ }
}
}

2.

<script type="text/javascript">
function closeWP() {
var Browser = navigator.appName;
var indexB = Browser.indexOf('Explorer'); if (indexB > 0) {
var indexV = navigator.userAgent.indexOf('MSIE') + 5;
var Version = navigator.userAgent.substring(indexV, indexV + 1); if (Version >= 7) {
window.open('', '_self', '');
window.close();
}
else if (Version == 6) {
window.opener = null;
window.close();
}
else {
window.opener = '';
window.close();
} }
else {
window.close();
}
}
</script>

http://stackoverflow.com/questions/57854/how-can-i-close-a-browser-window-without-receiving-the-do-you-want-to-close-thi

 

老式关闭当前页面js
window.opener=null;
window.open('','_self');
window.close();

但是关闭 Chrome 浏览器会有问题

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

搜了半天居然没有答案;所以我自己发上来以便别人需要

关闭当前页面js

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

 
其它参考:
A:
 
'p' is new window that i opened, i'm using jquery to test what browser you have, this is example when i open new window, populate it, call print dialog, then close it

it's not solution to original problem but just in case someone find it useful

if ( $.browser.msie ) p.location.reload();  //for IE if you want print dialog to show 
p.window.print(); 
if ( $.browser.msie || $.browser.opera || $.browser.mozilla) p.window.close(); 
else p.window.self.close(); //for chrome

 
B:

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 set
allow_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.

C: 
 

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.

参考网址:

 
 
 
 
发现最新版本ff和chrom还是有问题,临时采用如下方式处理:

function CloseWebPage(){
if (navigator.userAgent.indexOf("MSIE") > 0) {
if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
window.opener = null;
window.close();
}else {
window.open('', '_top');
window.top.close();
}
}
else if (navigator.userAgent.indexOf("Firefox")>0 || navigator.userAgent.indexOf("Chrome")> 0) {
//window.location.href = 'about:blank ';
window.location.href="about:blank";
window.close();
}
else {
window.opener=null;
window.open('','_self');
window.close();
}
}

js关闭浏览器的tab页(兼容)的更多相关文章

  1. js关闭浏览器窗口事件

    js关闭浏览器窗口 js关闭浏览器窗口,不弹出提示框.支持ie6+,火狐,谷歌等浏览器. <html> <head /> <body> <script typ ...

  2. 关于js关闭浏览器技术细谈

    前言:前端时间做项目遇到一个js的问题,需要使用js关闭浏览器,在原有js代码是有这样功能的, 代码如下 window.close(); 但是呢,chrome,firefox等中有时候会不起作用. 后 ...

  3. js关闭浏览器窗口及检查浏览器关闭事件

    js关闭浏览器窗口,不弹出提示框.支持ie6+,火狐,谷歌等浏览器,下面以一个示例为大家详细介绍下具体的实现方法,感兴趣的朋友可以参考下   js关闭浏览器窗口 js关闭浏览器窗口,不弹出提示框.支持 ...

  4. js关闭浏览器事件,js关闭浏览器提示及相关函数

    关于浏览器关闭事件的相关描述 有些朋友想在浏览器关闭的时候,弹出alert .confirm或者prompt等.实验证明,这种做法是失败的,原因是浏览器关闭事件自动屏蔽执行js的某些方法,从而防止恶意 ...

  5. js关闭浏览器

                                    不存在的 告诉策划:不好意思,这个需求实现不了. 旧版本浏览器有些支持window.close()方法,目前主流浏览器都不支持,就算让你 ...

  6. 浏览器,tab页显示隐藏的事件监听--页面可见性

    //监听浏览器tab切换,以便在tab切换之后,页面隐藏的时候,把弹幕停止 document.addEventListener('webkitvisibilitychange', function() ...

  7. js检测浏览器版本代码,兼容ie11

    原文:http://blog.csdn.net/tenkin/article/details/11640165 <script type="text/javascript"& ...

  8. JS 判断浏览器是否安装Flash 兼容IE、firefox

    /** * @Author: HTL * @Email: Huangyuan413026@163.com * @DateTime: 2016-06-02 11:37:05 * @Description ...

  9. 使用easyui为tab页增加右键菜单

    在使用easyui进行上左右布局一文中,我们已经使用easyui搭建起了一个简单的上左右布局.在使用的过程中,我们经常会遇到tab页打开的太多,但只能一个一个的关闭的烦恼,这个时候有没有想到eclip ...

随机推荐

  1. windowDialog销毁页面的问题

    [结贴] windowDialog销毁页面的问题 [复制链接]     Ghost丶 15 主题 91 帖子 200 积分 中级会员 积分 200 发消息 1# 电梯直达    发表于 2015-8- ...

  2. windows server 2008服务器 做raid0

    dell服务器,启动后,根据提示按F10进入raid设置,设置成raid0 我们的服务器是4块硬盘,每块600G,做raid0时,生成一个虚拟磁盘vdisk. 做完raid之后,做OS部署 重启服务器 ...

  3. 使用jQuery清空file文件域的解决方案(转)

    对一个文件域(input type=file)使用了验证后,我们总会希望把文件域中的值给清空了,在IE中,由于安全设置的原因,是不允许更改文件域的值的(也就是不能使用val("") ...

  4. eclipse中提示HttpServletRequest不能引用的解决办法

    两种解决方法: 1.右键点击项目->Build Path->Add Libraries..->Server Runtime 选择Apache Tomcat v8.0 2.右键点击项目 ...

  5. 64. ZigZag Conversion

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  6. win10激活

    听了同事忽悠,说x230这款还在保,可以直接装win10,自动激活,结果悲剧.送到联想工作站需要摧毁所有数据,然后还要2.3个小时,遂在网上找了个方法,先用着,只是不知道什么时候又变成黑户: 以管理员 ...

  7. 方法的重载(overload)和重写(override)的区别

    方法的重写Overriding和重载Overloading是Java多态性的不同表现.重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现.如 ...

  8. Protractor

    官网地址:http://www.protractortest.org/ 1. 预备环境 protractor 是一个 Node.js 程序,为了运行 protractor ,你首先需要 Node 环境 ...

  9. 华为OJ题目:扑克牌大小

    题目描述: 扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A.2各4张,小王1张,大王1张.牌面从小到大用如下字符和字符串表示(其中,小写joker表示小王,大写JOKER表示大王):3  ...

  10. 百度地图API示例之移动地图

    级别为6 级别为8 级别为12 代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Conten ...