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

How to close a browser window without receiving a message about closing this window?

The request appears when I use the window.close(); function window.close(); .

+71
javascript browser
Sep 11 '08 at 22:59
source share
18 answers
 window.open('', '_self', ''); window.close(); 

This works for me.

+74
Apr 28 2018-10-14T00:
source share

My friend ... there is a way, but the "hack" does not begin to describe it. You should mainly use the bug in IE 6 and 7.

It 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 stupid IE allows you to close the browser on this page.

Closing page:

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

Amazing yes ?!

+54
Sep 11 '09 at 16:54
source share

Scripts are forbidden to close the window opened by the user. This is considered a security risk. Although this is not a standard, all browser vendors follow this ( Mozilla Docs ). If this happens in some browsers, this is a security bug that (ideally) is fixed very quickly.

None of the hacks in the answers to this question work, and if someone comes up with another dirty hack, in the end it will also stop working.

I suggest you not to waste energy fighting this and adopt a method that the browser helps you so much; ask the user before you seem to break your page.

+38
Jul 20 '14 at 19:45
source share

Here is the Javascript function that I use to close the browser without asking or warning, it can also be called from Flash. It should be in the 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){ } } } 
+28
May 7 '13 at 6:54
source share

In the body tag:

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

To close the window:

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

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

+12
Dec 13 2018-10-18
source share

This works in Chrome 26, Internet Explorer 9 and Safari 5.1.7 ( without using a sub page, ala Nick's answer):

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

Nested window.open should make IE non-displayable. You want to close this window.

Unfortunately, Firefox cannot close the window.

+11
Apr 10 '13 at 5:37
source share

For security reasons, a window can only be closed in JavaScript if it has been opened by JavaScript. To close the window, you must open a new window with _self as the target, which will overwrite your current window and then close it (which you can do from the moment you open it using JavaScript).

 window.open('', '_self', ''); window.close(); 
+7
Aug 31 2018-11-21T00:
source share

From here :

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

You need to install window.opener on something, otherwise it will complain.

+6
Sep 11 '08 at 23:04
source share

Due to security improvements in IE, you cannot close a window if it is not open using a script. Thus, going around this will make the browser think that this page is opened using a script, and then to close the window. Below is the implementation.

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

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

+4
Sep 18 '13 at 4:40
source share

Create JavaScript Function

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

Now write this code and call the JavaScript function above

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

Or simply:

 <a href="" onclick="closeme();">close</a> 
+2
Dec 17 '12 at 12:30
source share

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

Sorry a little later, but I found a solution, at least for my case. Tested in Safari 11.0.3 and Google Chrome 64.0.3282.167

+2
Feb 18 '18 at 17:53
source share
 window.opener=window; window.close(); 
+1
Apr 03 '09 at 9:35
source share

The best solution I found:

 this.focus(); self.opener=this; self.close(); 
0
Sep 11 '08 at 23:00
source share

The browser complains because you use JavaScript to close a window that was not opened using JavaScript, i.e. window.open('foo.html'); .

0
Sep 11 '08 at 23:05
source share

Put the following code in ASPX.

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

Put the following code in the code per click.

 string myclosescript = "<script language='javascript' type='text/javascript'>CloseWindow();</script>"; Page.ClientScript.RegisterStartupScript(GetType(), "myclosescript", myclosescript); 

If you don't have processing before closing, you can directly put the following code in ASPX itself in the clickTick tag.

 OnClientClick="CloseWindow();" 

Hope this helps.

0
Nov 11 '14 at 11:44
source share

This will work:

 <script type="text/javascript"> function closeWindowNoPrompt() { window.open('', '_parent', ''); window.close(); } </script> 
0
Feb 11 '16 at 9:50
source share

In my situation, the following code was embedded in 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 wrote the following

 PreventExitPop = false 

This solved the problem. So, find out the JavaScript code and find the variable and assign them to the corresponding "value", which in my case was "false"

0
Apr 17 '17 at 13:11
source share

I am going to publish this because it is what I am currently using for my site and it works in both Google Chrome and IE 10 without any pop-up messages:

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

I have a function on my site that I want to run in order to save the on / off variable in a session without directly going to a new page, so I just open a tiny pop-up web page. Then the web page is immediately closed by the function onload="window.close();" .

-2
Oct 08 '15 at 14:23
source share



All Articles