Anatoly Lubarsky Logo
programming, design, implementation, integration, games, music, web, mobile

Javascript: How to Close Browser Window (window.close()) Without Warning

There is function window.close() in javascript to close browser window. If we call it from the main browser window (not the one opened with window.open()) - we get:


"The page is trying to close the window" warning message.


This warning message shows up every time one tries to close browser window with window.close() function. It seems impossible to close the most outer main browser window and not to get this message.


But wait... window.close() function implementation is just so simple. It checks window.opener. If "opener" equals empty string it suspects that we talk about parent window (not the one that was opened with window.open(...)) and pop-ups warning. And it is easy to work around:


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

If you want to catch browser-close event - see javascript: close browser event tip.


Related Posts:

Thursday, June 10, 2004 1:22 AM

Comments

# re: HowTo: Close Parent window without warning (IE)
Great!! That's what I've been trying to find!

7/6/2004 10:02 PM by Yu

# re: HowTo: Close Parent window without warning (IE)
Thanks been looking for a work around on and off for a long time.

nice work

Jnk

7/9/2004 10:17 PM by Jnkyrd

# re: HowTo: Close Parent window without warning (IE)
Fantastic !!!

7/20/2004 1:13 PM by Piotr B.

# re: HowTo: Close Parent window without warning (IE)
javascript:self.close();

will close the primary window without a warrning when placed on a button.

7/21/2004 4:47 AM by Clinton Storer

# re: HowTo: Close Parent window without warning (IE)
sorry my mistake no it dosent

7/21/2004 4:48 AM by Clinton Storer

# re: HowTo: Close Parent window without warning (IE)
Thank you, i have tried a lot of ways but all of them react badly with the javascript i already have on the page. This works so well and is so simple.

Thanks duder.

Maxwell

7/30/2004 2:48 PM by Maxwell

# re: HowTo: Close Parent window without warning (IE)
Great.
Thanks a lot

8/17/2004 6:18 PM by phora

# re: HowTo: Close Parent window without warning (IE)
This works great. Thanks a bunch!

8/26/2004 2:52 AM by Bernd

# re: Javascript: Close Parent window without warning (IE)
You are the man! - don't forget it

3/4/2005 6:22 PM by Wez

# re: Javascript: Close Parent window without warning (IE)
does anyone have a website example? i tried the code above and it did not work! am i missing something?

3/29/2005 9:52 PM by nemarc

# re: Javascript: Close Parent window without warning (IE)
nemarc: just try to write this down in empty html

3/29/2005 10:24 PM by Anatoly Lubarsky

# re: Javascript: Close Parent window without warning (IE)
Thanks a lot!

4/13/2005 5:47 AM by power

# re: Javascript: Close Parent window without warning (IE)
Dude, thanks a lot.

6/2/2005 1:06 AM by Ash

# re: Javascript: Close Parent window without warning (IE)
no self.close() doesnt stop the warning from appearing..please give some other solution.

7/26/2005 12:29 PM by tuli

# Traffic Analysis

8/5/2005 4:58 PM by Anatoly Lubarsky: Weblog

# re: Javascript: Close Parent window without warning (IE)
Does it work for Mozilla Firefox?

10/7/2005 6:44 PM by gerbarruz

# re: Javascript: Close Parent window without warning (IE)
no

10/7/2005 7:14 PM by Anatoly Lubarsky

# re: Javascript: Close Parent window without warning (IE)
<html>
<head>
<title>Subok</title>
<script language="JavaScript">
<!--

function OpenMyWin(){
window.open('index.html', '');
window.close();
}

// -->
</script>
</head>

<body onload="window.opener='';OpenMyWin()">
</body>
</html>

10/12/2005 3:26 AM by Erwin

# re: Javascript: Close Parent window without warning (IE)
Great! Thank you!

10/20/2005 6:43 PM by venkat

# re: Javascript: Close Parent window without warning (IE)
Everything genius is simple !!!!!
Great job.
Thank you very much !!!!

10/28/2005 6:18 PM by Eugene

# re: Javascript: Close Parent window without warning (IE)
That works wonderfully, THANK YOU!

12/11/2005 11:35 PM by Clinton

# re: Javascript: Close Parent window without warning (IE)
This really works for me!!
Many thx!!

12/16/2005 1:52 AM by Bosco

# re: Javascript: Close Parent window without warning (IE)
Thanks, It is really good trick to fool the computer.

12/28/2005 2:30 PM by Rohit

# re: Javascript: Close Parent window without warning (IE)
This is right code and simple

<script language="JavaScript" type="text/javascript">
<!--
function closeW()
{
window.opener = self;
window.close();
}
// -->
</script>

12/30/2005 10:43 AM by Andy Nguyen

# re: Javascript: Close Parent window without warning (IE)
Great job.
This really works for me.............
Thank you very much !!!!

1/24/2006 11:41 PM by Vamshi Rapolu

# re: Javascript: Close Parent window with
I recently came up with a function to make window.close() work in FireFox :)

Find out why and get the code on my blog:
http://www.interwebby.com/blog/2006/02/04/3/

2/10/2006 8:19 AM by Marc

# re: Javascript: How to close browser win
Fantastic stuff: so simple, so effective

6/5/2006 7:21 PM by david

# re: Javascript: How to close browser window (window.close()) without warning
This code works in both IE and FF ----

<html>

<head>
<SCRIPT language="JavaScript">
<!--
var browserName=navigator.appName;
if (browserName=="Netscape") {

function closynoshowsme()
{
window.open('','_parent','');
window.close();}
}
else {
if (browserName=="Microsoft Internet Explorer")
{
function closynoshowsme()
{
window.opener = "whocares";
window.close();
}
}
}

//-->
</SCRIPT>

</head>

<body>
<a href="javascript: closynoshowsme();">Close Window</a><br /><br /><br />
<button type="button" onclick="closynoshowsme();">Close this window</button>
</body>
</html>




6/16/2006 2:33 AM by Mike Kirk

# re: Javascript: How to close browser window (window.close()) without warning
Thanku Very much it is so simple

10/18/2006 1:16 PM by Rajesh

# re: Javascript: How to close browser window (window.close()) without warning
great yaar,thnx very much

11/8/2006 7:01 AM by poornima

# re: Javascript: How to close browser window (window.close()) without warning
really superb thank U ...& ..God bless U

11/22/2006 3:24 PM by sujithfem

# re: Javascript: How to close browser window (window.close()) without warning
how do you close the browser in the mobile ie envir? It does not react to window.close

11/27/2006 4:33 AM by inkineu

# re: Javascript: How to close browser window (window.close()) without warning
Any change required for IE 7.0? Thanks a lot!

12/6/2006 9:36 PM by Asdecopas

# re: Javascript: How to close browser window (window.close()) without warning
Asdecopas: no

12/7/2006 1:00 AM by Anatoly Lubarsky

# re: Javascript: How to close browser window (window.close()) without warning
Hi there, I tried to use the code above, and it does not work it keeps warning me that the page is being closed.. I'm using IE7 on Windows XP.. is there something else I need to do?, thanks..

1/18/2007 4:22 PM by Serch

# re: Javascript: How to close browser window (window.close()) without warning
For IE7 it seems that the firefox solution works.
so to close the window on both IE6 & IE7 use the folowing code:

window.opener='X';
window.open('','_parent','');
window.close();

1/29/2007 10:02 AM by Ziv

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
Sei un Grande
You are Great !

5/29/2007 12:26 PM by Gen & Luke

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
Helpful information.
Thank you!

6/11/2007 1:15 PM by Jörg

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
It didn't work in Netscape 7.0. How do I include all browsers?

6/26/2007 6:37 PM by Teresa

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
if you want to close window using a gif button
<A
onclick=top.close() href="javascript:void(0)"><IMG height=11 src="close.gif"
width=11 border=0></A>
or else use
<A
onclick=top.close() href="javascript:void(0)">Exit!!</A>
or else if you want to close window onload
for php users:
echo "<body onload=top.close() href=\"javascript:void(0)\ content='10\">";
for html:
<body onload=top.close() href="javascript:void(0) content='10">

6/30/2007 9:00 PM by Ahsan

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
if some how above mentioned does not works mail at ahsan.smiles@gmail.com for info or visit forum.vo3.net for more info

6/30/2007 9:03 PM by Ahsan.smiles@gmail.com

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
The following solution for suppressing the warning message in IE6 & IE7 by Ziv is working excellent. Thanks Ziv!!

For IE7 it seems that the firefox solution works.
so to close the window on both IE6 & IE7 use the folowing code:

window.opener='X';
window.open('','_parent','');
window.close();

8/27/2007 9:54 PM by Naveen

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
Does it work in Mac OS X ? Firefox for Mac, or Internet Explorer for Mac?

3/10/2008 10:58 PM by For-Mac

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
It doesnt seem to work on firefox.
Any crose browser solutions?

3/17/2008 2:22 AM by Ofer

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
I have the same problem, I've tried different scripts, But nothing works... When I try it in IE7, It keeps on asking if I want to close the window... Can anyone help me?
this is where my file is: www.thatsteve.be/menu/start.htm

The thing is, I want to open a website in a window without menu or adresbar, or anything... just a window...
So I've made a normal page that calls a javascript to open a popup... In this popup my website shows.... So far so good.... Now, when the popup has loaded, I want the first window to close.... And that's when I get the message... Please help me...

Thanks in advance.
Steve

3/18/2008 10:08 PM by Steve

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
Hello.. First of all... my english isn't very well. I'm sorry for this..
my problem is like this....
I have a website that designed by visual studio 2005 c#
I open a parent window. the window has a child window.
I'd like close the parent window when the child window open.
I close the parent window with javascript.. that works good in Internet
Explorer but it doesn't work in mozilla firefox. the window cannot close

thank you for your helps bye

4/18/2008 2:17 PM by taner

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
Hi

Here is the code to make close the parent window without warning, in both IE and Firefox. Paste it in an HTML and enjoy

<html>
<head>
<script language="javascript" type="text/javascript">
function closeWindow() {
//uncomment to open a new window and close this parent window without warning
//var newwin=window.open("popUp.htm",'popup','');
if(navigator.appName=="Microsoft Internet Explorer") {
this.focus();self.opener = this;self.close(); }
else { window.open('','_parent',''); window.close(); }
}
</script>
</head>
<body>
<a href="javascript:closeWindow();">Close Window</a>
</body>
</html>

7/2/2008 7:10 PM by Cooldev

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
Yes! I want to close browser screen and return back to the web browser icon.

8/23/2008 6:20 PM by cedricwalker1

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
excellent

1/4/2009 12:31 PM by coder

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
awesome

1/4/2009 12:31 PM by coder

# re: Javascript: How to Close Browser Window (window.close()) Without Warning
Ziz you are my hero. Can I have your autograph?

1/30/2009 11:42 PM by Hairless

# Close browser | LuxonBiz
Close browser | LuxonBiz

6/24/2010 11:23 AM by Pingback/TrackBack

# avascript: How to close browser window (window.close()) without warning &laquo; Asepspmn&#8217;s Weblog
avascript: How to close browser window (window.close()) without warning &laquo; Asepspmn&#8217;s Weblog

8/26/2010 12:09 PM by Pingback/TrackBack

# How to render Fusioncharts completely on the server &#8211; Murray Picton
How to render Fusioncharts completely on the server &#8211; Murray Picton

9/25/2010 11:56 AM by Pingback/TrackBack

# Wage web &raquo; How Do You Close a Browser
Wage web &raquo; How Do You Close a Browser

12/2/2010 12:44 PM by Pingback/TrackBack

# JavaScript
JavaScript

3/13/2011 10:44 AM by Blog J.Schweiss

# Window close | Sheilahirt
Window close | Sheilahirt

1/4/2012 9:58 AM by Pingback/TrackBack

Login

Fosimo
Get Fosimo on CNET Download.com!
Subscribe via RSS

Article Categories

.Net Framework
ASP.NET Tips
C# Win32 API
HTML, CSS, Web
Javascript Tips
MSSQL Tips
System
System.Net
WebServices

Archives

(01) July, 2012
(01) April, 2012
(01) September, 2011
(01) August, 2011
(03) May, 2011
(01) March, 2011
(02) December, 2010
(01) November, 2010
(01) October, 2010
(01) September, 2010
(01) June, 2010
(01) May, 2010
(02) March, 2010
(01) January, 2010
(02) December, 2009
(03) September, 2009
(03) August, 2009
(09) July, 2009
(04) June, 2009
(04) May, 2009
(02) April, 2009
(05) March, 2009
(03) February, 2009
(03) January, 2009
(06) December, 2008
(04) November, 2008
(06) October, 2008
(07) September, 2008
(09) August, 2008
(05) July, 2008
(05) June, 2008
(07) May, 2008
(06) April, 2008
(03) March, 2008
(02) February, 2008
(04) January, 2008
(03) December, 2007
(05) November, 2007
(06) October, 2007
(07) September, 2007
(13) August, 2007
(11) July, 2007
(14) June, 2007
(16) May, 2007
(14) April, 2007
(10) March, 2007
(17) February, 2007
(21) January, 2007
(17) December, 2006
(14) November, 2006
(13) October, 2006
(13) September, 2006
(14) August, 2006
(12) July, 2006
(11) June, 2006
(12) May, 2006
(22) April, 2006
(36) March, 2006
(14) February, 2006
(14) January, 2006
(20) December, 2005
(17) November, 2005
(17) October, 2005
(17) September, 2005
(14) August, 2005
(18) July, 2005
(12) June, 2005
(17) May, 2005
(13) April, 2005
(22) March, 2005
(12) February, 2005
(14) January, 2005
(19) December, 2004
(15) November, 2004
(13) October, 2004
(16) September, 2004
(12) August, 2004
(29) July, 2004
(25) June, 2004
(33) May, 2004
(26) April, 2004
(18) March, 2004
(11) February, 2004
(07) January, 2004
(03) December, 2003
(02) November, 2003

Post Categories

.Net and C#
Android
Antispam
App. Development
Architecture
ASP.NET
Blogging
Delicer
Fosimo
Fun
Google
iOS
Javascript
Misc.
MSSQL
Music
Performance
Roller
Social Networks
Tools
Visual Studio
Web 2.0
WebServices
Yahoo

About Me

linkedin Profile
Recs
Recs Books
Who am I

My Sites

onecone
x2line blogs