失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > html 关闭当前tab页面 js关闭浏览器的tab页(兼容)

html 关闭当前tab页面 js关闭浏览器的tab页(兼容)

时间:2024-05-12 16:01:57

相关推荐

html 关闭当前tab页面 js关闭浏览器的tab页(兼容)

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

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

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

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

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

由/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 referencedwindowis 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 withwindow.open()

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

var openedWindow;

function openWindow()

{

openedWindow = window.open(‘moreinfo.htm‘);

}

function closeOpenedWindow()

{

openedWindow.close();

}

Closing the current window

When you call thewindowobject‘sclose()method directly, rather than callingclose()on awindowinstance, the browser will close the frontmost window, whether your script created that window or not.

function closeCurrentWindow()

{

window.close();

}

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

方案:

1.

functioncloseWindows(){varbrowserName =navigator.appName;varbrowserVer =parseInt(navigator.appVersion);//alert(browserName + " : "+browserVer);//document.getElementById("flashContent").innerHTML = "You have been logged out of the Game. Please Close Your Browser Window.";if(browserName =="Microsoft Internet Explorer"){varie7 =(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 IE6this.focus();self.opener =this;self.close();}}else{//For NON-IE Browsers except Firefox which doesnt support Auto Closetry{this.focus();self.opener =this;self.close();}catch(e){}try{window.open(‘‘,‘_self‘,‘‘);window.close();}catch(e){}}}

2.

functioncloseWP(){varBrowser=navigator.appName;varindexB =Browser.indexOf(‘Explorer‘);if(indexB >0){varindexV =navigator.userAgent.indexOf(‘MSIE‘)+5;varVersion=navigator.userAgent.substring(indexV,indexV +1);if(Version>=7){window.open(‘‘,‘_self‘,‘‘);window.close();}elseif(Version==6){window.opener =null;window.close();}else{window.opener =‘‘;window.close();}}else{window.close();}}

老式关闭当前页面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.

Theclose()method on Window objects should, if all the following conditions are met, close the browsing contextA:

The corresponding browsing contextAisscript-closable.

The browsing context of the incumbent script is familiar with the browsing contextA.

The browsing context of the incumbent script is allowed to navigate the browsing contextA.

A browsing context isscript-closableif it is an auxiliary browsing context thatwas 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 thewindow.openmethod.

If you try to usewindow.closefrom 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 bywindow.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:

Chromeis currently vulnerable to the "self redirection" exploit. So code like this willcurrentlywork 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.

Firefoxis secure against that exploit. So, the only javascript way is to cripple the security settings, one browser at a time.

You can open upabout:configand set

allow_scripts_to_close_windowstotrue.

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 usingwindow.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 meansThe hacky workarounds(Brock Adams‘s answer)may not workin 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,functioncallback)

method to remove it. Chrome extension windows API can be found atchrome.windows.

Actually my chrome extensionMarkViewwas 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 createdthis 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();

}

}

如果觉得《html 关闭当前tab页面 js关闭浏览器的tab页(兼容)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。