首页 > 上网技巧 > 电脑小技巧 > chrome window.close() 无法使用怎么办

chrome window.close() 无法使用怎么办

时间:2015-04-23 10:59 作者:QQ地带 我要评论

 

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

脚本可以关闭,打开的窗户。

 

 

由于在脚本中使用了 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();

 

标签: chrome JS
顶一下
(0)
0%
踩一下
(0)
0%

Google提供的广告