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

0 votes
asked Sep 11, 2008 by derek

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.

15 Answers

0 votes
answered by jbabey

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();
0 votes
answered Jan 11, 2008 by derek

The best solution I have found is:

this.focus();
self.opener=this;
self.close();
0 votes
answered Jan 11, 2008 by bill-ayakatubby

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');.

0 votes
answered Sep 11, 2008 by harley-holcombe

From here:

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

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

0 votes
answered Sep 11, 2009 by nick

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

0 votes
answered Sep 13, 2010 by jimb

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

0 votes
answered Sep 28, 2010 by arabam
window.open('', '_self', ''); window.close();

This works for me.

0 votes
answered Jan 17, 2012 by niloofar

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>
0 votes
answered Jan 18, 2013 by vivek

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

0 votes
answered Sep 7, 2013 by kuldip-d-gandhi

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

            }
        }
    }
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...