<p>How can I close a browser window without receiving the <em>Do you want to close this window</em> prompt?</p>

<p>The prompt occurs when I use the <code>window.close();</code> function.</p>

[{'answer_id': 57857, 'author': 'Derek', 'author_id': 5440, 'author_profile': 'https://Stackoverflow.com/users/5440', 'pm_score': -1, 'selected': False, 'text': '<p>The best solution I have found is:</p>\n\n<pre><code>this.focus();\nself.opener=this;\nself.close();\n</code></pre>\n'}, {'answer_id': 57868, 'author': 'Harley Holcombe', 'author_id': 1057, 'author_profile': 'https://Stackoverflow.com/users/1057', 'pm_score': 3, 'selected': False, 'text': '<p>From <a href="http://blogs.x2line.com/al/articles/350.aspx" rel="nofollow noreferrer">here</a>:</p>\n\n<pre><code>&lt;a href="javascript:window.opener=\'x\';window.close();"&gt;Close&lt;/a&gt;\n</code></pre>\n\n<p>You need to set <code>window.opener</code> to something, otherwise it complains.</p>\n'}, {'answer_id': 57872, 'author': 'Billy Jo', 'author_id': 3447, 'author_profile': 'https://Stackoverflow.com/users/3447', 'pm_score': 0, 'selected': False, 'text': "<p>The browser is complaining because you're using JavaScript to close a window that wasn't opened with JavaScript, i.e. <code>window.open('foo.html');</code>.</p>\n"}, {'answer_id': 713230, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<pre><code>window.opener=window;\nwindow.close();\n</code></pre>\n'}, {'answer_id': 1412097, 'author': 'Nick', 'author_id': 56256, 'author_profile': 'https://Stackoverflow.com/users/56256', 'pm_score': 6, 'selected': False, 'text': '<p>My friend... there is a way but "hack" does not begin to describe it. You have to basically exploit a bug in IE 6 &amp; 7. </p>\n\n<p>Works every time!</p>\n\n<p>Instead of calling <code>window.close()</code>, redirect to another page. </p>\n\n<p>Opening Page:</p>\n\n<pre><code>alert("No whammies!");\nwindow.open("closer.htm", \'_self\');\n</code></pre>\n\n<p>Redirect to another page. This fools IE into letting you close the browser on this page. </p>\n\n<p>Closing Page:</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\n    window.close();\n&lt;/script&gt;\n</code></pre>\n\n<p>Awesome huh?!</p>\n'}, {'answer_id': 2730590, 'author': 'Arabam', 'author_id': 326958, 'author_profile': 'https://Stackoverflow.com/users/326958', 'pm_score': 6, 'selected': False, 'text': "<pre><code>window.open('', '_self', ''); window.close();\n</code></pre>\n\n<p>This works for me.</p>\n"}, {'answer_id': 4432315, 'author': 'JimB', 'author_id': 256960, 'author_profile': 'https://Stackoverflow.com/users/256960', 'pm_score': 4, 'selected': False, 'text': '<p>In the body tag:</p>\n\n<pre><code>&lt;body onload="window.open(\'\', \'_self\', \'\');"&gt;\n</code></pre>\n\n<p>To close the window:</p>\n\n<pre><code>&lt;a href="javascript:window.close();"&gt;\n</code></pre>\n\n<p>Tested on Safari 4.0.5, FF for Mac 3.6, IE 8.0, and FF for Windows 3.5</p>\n'}, {'answer_id': 7262745, 'author': 'jbabey', 'author_id': 386152, 'author_profile': 'https://Stackoverflow.com/users/386152', 'pm_score': 3, 'selected': False, 'text': "<p>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 <code>_self</code> as the target, which will overwrite your current window, and then close that one (which you can do since it was opened via JavaScript).</p>\n\n<pre><code>window.open('', '_self', '');\nwindow.close();\n</code></pre>\n"}, {'answer_id': 13914234, 'author': 'Niloofar', 'author_id': 1814925, 'author_profile': 'https://Stackoverflow.com/users/1814925', 'pm_score': 2, 'selected': False, 'text': '<p>Create a JavaScript function</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\n    function closeme() {\n        window.open(\'\', \'_self\', \'\');\n        window.close();\n    }\n&lt;/script&gt;\n</code></pre>\n\n<p>Now write this code and call the above JavaScript function</p>\n\n<pre><code>&lt;a href="Help.aspx" target="_blank" onclick="closeme();"&gt;Help&lt;/a&gt;\n</code></pre>\n\n<p>Or simply:</p>\n\n<pre><code>&lt;a href="" onclick="closeme();"&gt;close&lt;/a&gt;\n</code></pre>\n'}, {'answer_id': 15917784, 'author': 'Danny Beckett', 'author_id': 1563422, 'author_profile': 'https://Stackoverflow.com/users/1563422', 'pm_score': 4, 'selected': False, 'text': '<p>This works in Chrome 26, Internet Explorer 9 and Safari 5.1.7 (<strong>without</strong> the use of a helper page, ala Nick\'s answer):</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\n    window.open(\'javascript:window.open("", "_self", "");window.close();\', \'_self\');\n&lt;/script&gt;\n</code></pre>\n\n<p>The nested <code>window.open</code> is to make IE not display the <em>Do you want to close this window</em> prompt.</p>\n\n<p>Unfortunately it is impossible to get Firefox to close the window.</p>\n'}, {'answer_id': 16412945, 'author': 'Kuldip D Gandhi', 'author_id': 2357159, 'author_profile': 'https://Stackoverflow.com/users/2357159', 'pm_score': 5, 'selected': False, 'text': '<p>Here is Javascript function which I use to close browser without Prompt or Warning, it can also be called from Flash.\nIt should be in html file.</p>\n\n<pre><code>    function closeWindows() {\n         var browserName = navigator.appName;\n         var browserVer = parseInt(navigator.appVersion);\n         //alert(browserName + " : "+browserVer);\n\n         //document.getElementById("flashContent").innerHTML = "&lt;br&gt;&amp;nbsp;&lt;font face=\'Arial\' color=\'blue\' size=\'2\'&gt;&lt;b&gt; You have been logged out of the Game. Please Close Your Browser Window.&lt;/b&gt;&lt;/font&gt;";\n\n         if(browserName == "Microsoft Internet Explorer"){\n             var ie7 = (document.all &amp;&amp; !window.opera &amp;&amp; window.XMLHttpRequest) ? true : false;  \n             if (ie7)\n             {  \n               //This method is required to close a window without any prompt for IE7 &amp; greater versions.\n               window.open(\'\',\'_parent\',\'\');\n               window.close();\n             }\n            else\n             {\n               //This method is required to close a window without any prompt for IE6\n               this.focus();\n               self.opener = this;\n               self.close();\n             }\n        }else{  \n            //For NON-IE Browsers except Firefox which doesnt support Auto Close\n            try{\n                this.focus();\n                self.opener = this;\n                self.close();\n            }\n            catch(e){\n\n            }\n\n            try{\n                window.open(\'\',\'_self\',\'\');\n                window.close();\n            }\n            catch(e){\n\n            }\n        }\n    }\n</code></pre>\n'}, {'answer_id': 18863981, 'author': 'Vivek', 'author_id': 2373500, 'author_profile': 'https://Stackoverflow.com/users/2373500', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n\n<p>Try this, it works like a charm!<br>\n<strong><em><a href="http://www.dotnetbull.com/2011/12/closing-window-without-prompt-in.html" rel="nofollow">javascript close current window without prompt IE</a></em></strong></p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\nfunction closeWP() {\n var Browser = navigator.appName;\n var indexB = Browser.indexOf(\'Explorer\');\n\n if (indexB &gt; 0) {\n    var indexV = navigator.userAgent.indexOf(\'MSIE\') + 5;\n    var Version = navigator.userAgent.substring(indexV, indexV + 1);\n\n    if (Version &gt;= 7) {\n        window.open(\'\', \'_self\', \'\');\n        window.close();\n    }\n    else if (Version == 6) {\n        window.opener = null;\n        window.close();\n    }\n    else {\n        window.opener = \'\';\n        window.close();\n    }\n\n }\nelse {\n    window.close();\n }\n}\n&lt;/script&gt;\n</code></pre>\n\n<h2><a href="http://www.dotnetbull.com/2011/12/closing-window-without-prompt-in.html" rel="nofollow">javascript close current window without prompt IE</a></h2>\n'}, {'answer_id': 24854246, 'author': 'rvighne', 'author_id': 1079573, 'author_profile': 'https://Stackoverflow.com/users/1079573', 'pm_score': 6, 'selected': False, 'text': '<p>Scripts are <em>not allowed</em> 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 (<a href="https://developer.mozilla.org/en-US/docs/Web/API/Window.close">Mozilla docs</a>). If this happens in some browsers, it\'s a security bug that (ideally) gets patched very quickly.</p>\n\n<p>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.</p>\n\n<p>I suggest you don\'t waste energy fighting this and embrace the method that the browser so helpfully gives you &mdash; <em>ask</em> the user before you seemingly crash their page.</p>\n'}, {'answer_id': 26864113, 'author': 'Kamleshkumar Gujarathi', 'author_id': 4239394, 'author_profile': 'https://Stackoverflow.com/users/4239394', 'pm_score': 0, 'selected': False, 'text': '<p>Place the following code in the ASPX.</p>\n\n<pre><code>&lt;script language=javascript&gt;\nfunction CloseWindow() \n{\n    window.open(\'\', \'_self\', \'\');\n    window.close();\n}\n&lt;/script&gt;\n</code></pre>\n\n<p>Place the following code in the code behind button click event.</p>\n\n<pre><code>string myclosescript = "&lt;script language=\'javascript\' type=\'text/javascript\'&gt;CloseWindow();&lt;/script&gt;";\n\nPage.ClientScript.RegisterStartupScript(GetType(), "myclosescript", myclosescript);\n</code></pre>\n\n<p>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.</p>\n\n<pre><code>OnClientClick="CloseWindow();"\n</code></pre>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 33018553, 'author': 'Logan Hasbrouck', 'author_id': 3609893, 'author_profile': 'https://Stackoverflow.com/users/3609893', 'pm_score': -1, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>&lt;html&gt;\n    &lt;head&gt;\n    &lt;/head&gt;\n    &lt;body onload="window.close();"&gt;\n    &lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>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 <code>onload="window.close();"</code> function.</p>\n'}, {'answer_id': 35335816, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>This will work :</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\nfunction closeWindowNoPrompt()\n{\nwindow.open(\'\', \'_parent\', \'\');\nwindow.close();\n}\n&lt;/script&gt;\n</code></pre>\n'}, {'answer_id': 43452197, 'author': 'nurmurat', 'author_id': 887620, 'author_profile': 'https://Stackoverflow.com/users/887620', 'pm_score': 1, 'selected': False, 'text': '<p>In my situation the following code was embedded into a php file.</p>\n\n<pre><code>var PreventExitPop = true;\nfunction ExitPop() {\n  if (PreventExitPop != false) {\n    return "Hold your horses! \\n\\nTake the time to reserve your place.Registrations might become paid or closed completely to newcomers!"\n  }\n}\nwindow.onbeforeunload = ExitPop;\n</code></pre>\n\n<p>So I opened the console and write the following </p>\n\n<pre><code>PreventExitPop = false\n</code></pre>\n\n<p>This solved the problem.\nSo, find out the JavaScript code and find the variable(s) and assign them to an appropriate "value" which in my case was "false"</p>\n'}, {'answer_id': 48854816, 'author': 'Mada Aryakusumah', 'author_id': 1837643, 'author_profile': 'https://Stackoverflow.com/users/1837643', 'pm_score': 3, 'selected': False, 'text': '<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>window.open(\'\', \'_self\', \'\').close();</code></pre>\r\n</div>\r\n</div>\r\n</p>\n\n<p>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</p>\n'}]
['https://Stackoverflow.com/questions/57854', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5440/']