How can I close a browser window without receiving

2019-01-01 01:20发布

问题:

How can I close a browser window without receiving the Do you want to close this window prompt?

The prompt occurs when I use the window.close(); function.

回答1:

window.open(\'\', \'_self\', \'\'); window.close();

This works for me.



回答2:

My friend... there is a way but \"hack\" does not begin to describe it. You have to basically exploit a bug in IE 6 & 7.

Works every time!

Instead of calling window.close(), redirect to another page.

Opening Page:

alert(\"No whammies!\");
window.open(\"closer.htm\", \'_self\');

Redirect to another page. This fools IE into letting you close the browser on this page.

Closing Page:

<script type=\"text/javascript\">
    window.close();
</script>

Awesome huh?!



回答3:

Scripts are not allowed to close a window that a user opened. This is considered a security risk. Though it isn\'t in any standard, all browser vendors follow this (Mozilla docs). If this happens in some browsers, it\'s a security bug that (ideally) gets patched very quickly.

None of the hacks in the answers on this question work any longer, and if someone would come up with another dirty hack, eventually it will stop working as well.

I suggest you don\'t waste energy fighting this and embrace the method that the browser so helpfully gives you — ask the user before you seemingly crash their page.



回答4:

Here is Javascript function which I use to close browser without Prompt or Warning, it can also be called from Flash. It should be in html file.

    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){

            }
        }
    }


回答5:

In the body tag:

<body onload=\"window.open(\'\', \'_self\', \'\');\">

To close the window:

<a href=\"javascript:window.close();\">

Tested on Safari 4.0.5, FF for Mac 3.6, IE 8.0, and FF for Windows 3.5



回答6:

This works in Chrome 26, Internet Explorer 9 and Safari 5.1.7 (without the use of a helper page, ala Nick\'s answer):

<script type=\"text/javascript\">
    window.open(\'javascript:window.open(\"\", \"_self\", \"\");window.close();\', \'_self\');
</script>

The nested window.open is to make IE not display the Do you want to close this window prompt.

Unfortunately it is impossible to get Firefox to close the window.



回答7:

For security reasons, a window can only be closed in JavaScript if it was opened by JavaScript. In order to close the window, you must open a new window with _self as the target, which will overwrite your current window, and then close that one (which you can do since it was opened via JavaScript).

window.open(\'\', \'_self\', \'\');
window.close();


回答8:

From here:

<a href=\"javascript:window.opener=\'x\';window.close();\">Close</a>

You need to set window.opener to something, otherwise it complains.



回答9:

Because of the security enhancements in IE, you can\'t close a window unless it is opened by a script. So the way around this will be to let the browser think that this page is opened using a script, and then to close the window. Below is the implementation.

Try this, it works like a charm!
javascript close current window without prompt IE

<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>

javascript close current window without prompt IE



回答10:

window.open(\'\', \'_self\', \'\').close();

Sorry a bit late here, but i found the solution, at least for my case. Tested on Safari 11.0.3 and Google Chrome 64.0.3282.167



回答11:

Create a JavaScript function

<script type=\"text/javascript\">
    function closeme() {
        window.open(\'\', \'_self\', \'\');
        window.close();
    }
</script>

Now write this code and call the above JavaScript function

<a href=\"Help.aspx\" target=\"_blank\" onclick=\"closeme();\">Help</a>

Or simply:

<a href=\"\" onclick=\"closeme();\">close</a>


回答12:

window.opener=window;
window.close();


回答13:

The browser is complaining because you\'re using JavaScript to close a window that wasn\'t opened with JavaScript, i.e. window.open(\'foo.html\');.



回答14:

The best solution I have found is:

this.focus();
self.opener=this;
self.close();


回答15:

Place the following code in the ASPX.

<script language=javascript>
function CloseWindow() 
{
    window.open(\'\', \'_self\', \'\');
    window.close();
}
</script>

Place the following code in the code behind button click event.

string myclosescript = \"<script language=\'javascript\' type=\'text/javascript\'>CloseWindow();</script>\";

Page.ClientScript.RegisterStartupScript(GetType(), \"myclosescript\", myclosescript);

If you dont have any processing before close then you can directly put the following code in the ASPX itself in the button click tag.

OnClientClick=\"CloseWindow();\"

Hope this helps.



回答16:

This will work :

<script type=\"text/javascript\">
function closeWindowNoPrompt()
{
window.open(\'\', \'_parent\', \'\');
window.close();
}
</script>


回答17:

In my situation the following code was embedded into a php file.

var PreventExitPop = true;
function ExitPop() {
  if (PreventExitPop != false) {
    return \"Hold your horses! \\n\\nTake the time to reserve your place.Registrations might become paid or closed completely to newcomers!\"
  }
}
window.onbeforeunload = ExitPop;

So I opened the console and write the following

PreventExitPop = false

This solved the problem. So, find out the JavaScript code and find the variable(s) and assign them to an appropriate \"value\" which in my case was \"false\"



回答18:

I am going to post this because this is what I am currently using for my site and it works in both Google Chrome and IE 10 without receiving any popup messages:

<html>
    <head>
    </head>
    <body onload=\"window.close();\">
    </body>
</html>

I have a function on my site that I want to run to save an on/off variable to session without directly going to a new page so I just open a tiny popup webpage. That webpage then closes itself immediately with the onload=\"window.close();\" function.