Close window without the prompt message in IE7

If you tried to close a window using javascript window.close() method in IE7, and as you may noticed a message will prompt "The Webpage you are viewing is trying to close the window. Do you want to close this window".

Because of the security enhancements in IE7, you can't close a window unless it is opened by a script. so the walkaround will be to let the browser thinks that this page is opened using a script then closing the window. below is the implementation.

1- Create a javascript function which will be called to close the window

<script language=javascript>

function CloseWindow()

{

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

window.close();

}

</script>

the code in bold is used to open a window in this case it's not defined into the current window. in this way, we let the browser thinks that the current window is opened using javascript, so when the window.close() is executed it will close it without the message being displayed.

Now you can try it by adding the below HTML code

<a href="" onclick="CloseWindow();">Testing Close Window</a>

Hope this post will help you.

Best Regards,

HC

 

Comments

# re: Close window without the prompt message in IE7

Friday, April 20, 2007 9:41 AM by Josh Stodola

Do you know if this works in IE6?  If not, I have always used the following method (it does not work for IE7):

window.opener = self;

window.close();

Regards,

-Josh

# re: Close window without the prompt message in IE7

Wednesday, April 25, 2007 2:31 AM by Mike

Just wanted to say thanks for this tip.  Works like a charm!

# re: Close window without the prompt message in IE7

Tuesday, May 01, 2007 1:59 PM by Sam

Thanx man. It works

# re: Close window without the prompt message in IE7

Thursday, May 03, 2007 9:59 AM by Marcel.

Thanks a lot

# re: Close window without the prompt message in IE7

Thursday, May 03, 2007 10:05 AM by Kevin

Finally, a slick solution to this common problem.  Thanks sooo much, works great.

# re: Close window without the prompt message in IE7

Thursday, May 03, 2007 4:16 PM by haissam

Thank you all for you comments.

Still i didnt test it under IE6, once i do i will surely provide my feedback.

Best Regards,

HC

# re: Close window without the prompt message in IE7

Friday, May 04, 2007 4:48 AM by AS

Thanks a lot for the cool tip!

# re: Close window without the prompt message in IE7

Saturday, May 05, 2007 1:39 AM by Meera

Its good.But any solution for all version of IE?

# re: Close window without the prompt message in IE7

Wednesday, May 09, 2007 3:43 AM by Jane

I'm the unlucky one, my window is not closing at all??? thanks.

# re: Close window without the prompt message in IE7

Wednesday, May 09, 2007 4:07 PM by haissam

Could you tell me which code you used and what you are trying to do!!

# re: Close window without the prompt message in IE7

Thursday, May 10, 2007 6:24 AM by KoSun

Thanx man. It works

# re: Close window without the prompt message in IE7

Wednesday, May 16, 2007 9:55 AM by Andy

Nice solution, but it doesn't work for a button on a window.showModalDialog window.  Any suggestions there?

# re: Close window without the prompt message in IE7

Thursday, May 17, 2007 1:28 AM by dstangirala

If I check for print preview of the page and close the same, this is opening the folder in which the HTML has been created.

# re: Close window without the prompt message in IE7

Thursday, May 17, 2007 1:28 AM by dstangirala

If I check for print preview of the page and close the same, this is opening the folder in which the HTML has been created. Can you pls let me know how to reslove that one?

# re: Close window without the prompt message in IE7

Thursday, May 17, 2007 2:09 PM by JRoeh

I tested this in IE 6 and it works for both versions.

THANKS!

# re: Close window without the prompt message in IE7

Tuesday, May 22, 2007 2:41 AM by Crash_Daemonicus

It works for other people on my website but not for Me!!

All IE7 will give me is "Object Expected"

# re: Close window without the prompt message in IE7

Tuesday, May 22, 2007 6:26 AM by kiran

this works well in IE 6 but i need the solution for IE7 any one getting the solutions for IE 7

# re: Close window without the prompt message in IE7

Wednesday, May 30, 2007 11:19 AM by Raj

Response.Write("<script language=\"JavaScript\">window.open('','_self','');window.close(\"" + "" + "\");</script>");

This works for IE 7.0.5730.11

HTML equivalent>>

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

window.close();

# re: Close window without the prompt message in IE7

Wednesday, May 30, 2007 6:09 PM by haissam

Raj Try not to use Response.Write to inject javascript because it messes up your HTML markup. However you can take a look at Page.ClientScript.RegisterStartupScript

Best Regards,

HC

# re: Close window without the prompt message in IE7

Monday, June 04, 2007 5:26 AM by balmydrizzle

doesn't work even in IE6, it does nothing

# re: Close window without the prompt message in IE7

Monday, June 04, 2007 5:28 AM by balmydrizzle

In my case it's a html generated by JSP, and it doesn't work...

# re: Close window without the prompt message in IE7

Sunday, July 01, 2007 1:31 PM by Ruby

thanks a million!!

was searching the net for over an hour till i found ur site n it solved my prob...

# re: Close window without the prompt message in IE7

Sunday, July 15, 2007 11:35 AM by Fred

Wow, really helpful. Thanks Haissam.

# re: Close window without the prompt message in IE7

Tuesday, July 17, 2007 9:28 PM by matjacks

Been looking everywhere for this workaround.  Thanks man.  This is really great!

# re: Close window without the prompt message in IE7

Friday, July 27, 2007 9:48 AM by Ashok

Hi i have the same problem please give me the suggestion

# re: Close window without the prompt message in IE7

Wednesday, August 01, 2007 3:43 PM by Dave

Thank you very much for the tip.

# re: Close window without the prompt message in IE7

Wednesday, August 01, 2007 9:15 PM by Thomas

Thanks, exactly what I was trying to figure out. Good Job!

# re: Close window without the prompt message in IE7

Wednesday, September 12, 2007 6:08 PM by Sebas Knight

great man! congrats to you!

# re: Close window without the prompt message in IE7

Wednesday, September 19, 2007 1:02 AM by Tie Cha

works great on IE7

# re: Close window without the prompt message in IE7

Wednesday, September 19, 2007 4:07 AM by haissam

Thank you all for your comments ....

# re: Close window without the prompt message in IE7

Monday, September 24, 2007 8:11 AM by BasharKokash

Hi Hissam,

I tried your code on a single webpaage and worked.

please check this post:

forums.asp.net/.../1161391.aspx

cause poeple their say that this works only if the window was a pop-up.

# re: Close window without the prompt message in IE7

Tuesday, September 25, 2007 9:23 AM by Ennio

Thanks man! Works like a charm!

# re: Close window without the prompt message in IE7

Wednesday, September 26, 2007 3:27 PM by Muhammad Akhtar

Excellant man,

It resolve the problem.

Thanks

# re: Close window without the prompt message in IE7

Tuesday, October 16, 2007 11:56 AM by Matt H

Here is a solution that handles both IE7 and IE6.

function closeWindow() {

     //var browserName = navigator.appName;

     //var browserVer = parseInt(navigator.appVersion);

     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

           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();

           }

}

</script>

<BODY onload='closeWindow()'>

# re: Close window without the prompt message in IE7

Thursday, November 22, 2007 2:09 PM by Oz

Thanks nice tip.

# re: Close window without the prompt message in IE7

Friday, December 07, 2007 5:19 AM by Rahul Pawar

I have tested on IE6 and IE7. both the cases the given below function works.

function closepop()

       {

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

           window.close();

       }

Thanks a lot.

# re: Close window without the prompt message in IE7

Friday, December 07, 2007 3:41 PM by GringoBob

thank you very much for this work around - works great with IE7

# re: Close window without the prompt message in IE7

Thursday, December 27, 2007 8:39 AM by Ricardo

It's a very simple and functional solution, Haissam. Congratulations!

Thanks!

# re: Close window without the prompt message in IE7

Thursday, January 03, 2008 6:20 AM by Nikhil

Thanks for your valuable code, it solved my problem.

# re: Close window without the prompt message in IE7

Friday, January 04, 2008 8:06 AM by Amit

This is fine solution

# re: Close window without the prompt message in IE7

Tuesday, January 08, 2008 2:00 PM by DaveS

I tried many, many other ways to do this. Finally a way to do this in IE7. Thanks Haissam it works great!

# re: Close window without the prompt message in IE7

Friday, January 11, 2008 12:34 PM by dave

It works for me with PHP, but it seems to be killing the session as well, which is not a good thing (user has to re-log in).  

# re: Close window without the prompt message in IE7

Tuesday, January 15, 2008 9:21 AM by Premjee

Thanks guys..This worked..!!!

# re: Close window without the prompt message in IE7

Saturday, January 19, 2008 10:10 AM by Colorian

I need to open a page in fullscreen AND close the current page, which isn't a popup. Can some1 tell me how this works? I tried a lot, but doesn't work. This is my code:

<script>

function fullwin(){

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

window.close();

window.open("menu.html","","fullscreen,scrollbars");

}

</script>

It works with a text you have to click on. (see www.to-elo.be/manthanein_met_mumett.htm )

Thanks!

# re: Close window without the prompt message in IE7

Monday, February 04, 2008 6:58 AM by Dawid

Thanks!!!

# re: Close window without the prompt message in IE7

Wednesday, February 06, 2008 4:41 PM by Lynze

Really needed this for my honours project, works like a charm ta v much.

# re: Close window without the prompt message in IE7

Wednesday, February 27, 2008 8:36 AM by Srinivas

HI boss,

Thanks your code for alertmessage for iE7 is excelent .

Thanks

# re: Close window without the prompt message in IE7

Monday, March 10, 2008 4:05 PM by HoGo

ThanX for your code about message in IE7!

You are the best!

# re: Close window without the prompt message in IE7

Friday, March 14, 2008 5:49 AM by haissam

Now Microsoft released the beta version for IE8. I still didnt download it. Let's hope it will work fine in IE8 also.

# re: Close window without the prompt message in IE7

Tuesday, March 18, 2008 5:20 AM by Rajamani

It really solved my problem. Thank you so much.

# &raquo; Close window without the prompt message in IE7 > 1656 - Unroll, Unfold

Pingback from  &raquo; Close window without the prompt message in IE7 > 1656 - Unroll, Unfold

# HTML Coding: ?Do You Want To Close This Window??

Wednesday, July 16, 2008 9:10 AM by HTML Coding: ?Do You Want To Close This Window??

Pingback from  HTML Coding: ?Do You Want To Close This Window??

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.