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

 

Published 20 April 2007 05:03 AM by Haissam

Comments

# Josh Stodola said on 20 April, 2007 09:41 AM

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

# Mike said on 25 April, 2007 02:31 AM

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

# Sam said on 01 May, 2007 01:59 PM

Thanx man. It works

# Marcel. said on 03 May, 2007 09:59 AM

Thanks a lot

# Kevin said on 03 May, 2007 10:05 AM

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

# Haissam said on 03 May, 2007 04:16 PM

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

# AS said on 04 May, 2007 04:48 AM

Thanks a lot for the cool tip!

# Meera said on 05 May, 2007 01:39 AM

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

# Jane said on 09 May, 2007 03:43 AM

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

# Haissam said on 09 May, 2007 04:07 PM

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

# KoSun said on 10 May, 2007 06:24 AM

Thanx man. It works

# Andy said on 16 May, 2007 09:55 AM

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

# dstangirala said on 17 May, 2007 01:28 AM

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.

# dstangirala said on 17 May, 2007 01:28 AM

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?

# JRoeh said on 17 May, 2007 02:09 PM

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

THANKS!

# Crash_Daemonicus said on 22 May, 2007 02:41 AM

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

All IE7 will give me is "Object Expected"

# kiran said on 22 May, 2007 06:26 AM

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

# Raj said on 30 May, 2007 11:19 AM

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

# Haissam said on 30 May, 2007 06:09 PM

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

# balmydrizzle said on 04 June, 2007 05:26 AM

doesn't work even in IE6, it does nothing

# balmydrizzle said on 04 June, 2007 05:28 AM

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

# Ruby said on 01 July, 2007 01:31 PM

thanks a million!!

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

# Fred said on 15 July, 2007 11:35 AM

Wow, really helpful. Thanks Haissam.

# matjacks said on 17 July, 2007 09:28 PM

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

# Ashok said on 27 July, 2007 09:48 AM

Hi i have the same problem please give me the suggestion

# Dave said on 01 August, 2007 03:43 PM

Thank you very much for the tip.

# Thomas said on 01 August, 2007 09:15 PM

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

# Sebas Knight said on 12 September, 2007 06:08 PM

great man! congrats to you!

# Tie Cha said on 19 September, 2007 01:02 AM

works great on IE7

# Haissam said on 19 September, 2007 04:07 AM

Thank you all for your comments ....

# BasharKokash said on 24 September, 2007 08:11 AM

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.

# Ennio said on 25 September, 2007 09:23 AM

Thanks man! Works like a charm!

# Muhammad Akhtar said on 26 September, 2007 03:27 PM

Excellant man,

It resolve the problem.

Thanks

# Matt H said on 16 October, 2007 11:56 AM

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

# Oz said on 22 November, 2007 02:09 PM

Thanks nice tip.

# Rahul Pawar said on 07 December, 2007 05:19 AM

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.

# GringoBob said on 07 December, 2007 03:41 PM

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

# Ricardo said on 27 December, 2007 08:39 AM

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

Thanks!

# Nikhil said on 03 January, 2008 06:20 AM

Thanks for your valuable code, it solved my problem.

# Amit said on 04 January, 2008 08:06 AM

This is fine solution

# DaveS said on 08 January, 2008 02:00 PM

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

# dave said on 11 January, 2008 12:34 PM

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

# Premjee said on 15 January, 2008 09:21 AM

Thanks guys..This worked..!!!

# Colorian said on 19 January, 2008 10:10 AM

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!

# Dawid said on 04 February, 2008 06:58 AM

Thanks!!!

# Lynze said on 06 February, 2008 04:41 PM

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

# Srinivas said on 27 February, 2008 08:36 AM

HI boss,

Thanks your code for alertmessage for iE7 is excelent .

Thanks

# HoGo said on 10 March, 2008 04:05 PM

ThanX for your code about message in IE7!

You are the best!

# Haissam said on 14 March, 2008 05:49 AM

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

# Rajamani said on 18 March, 2008 05:20 AM

It really solved my problem. Thank you so much.

# » Close window without the prompt message in IE7 > 1656 - Unroll, Unfold said on 16 April, 2008 04:54 AM

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

# HTML Coding: ?Do You Want To Close This Window?? said on 16 July, 2008 09:10 AM

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

# Jim said on 19 August, 2009 09:18 AM

Works in IE6, IE7, and IE8.

# Jim said on 19 August, 2009 09:19 AM

Thanks!!  It works in IE6, IE7, and IE8.

# Roy said on 26 August, 2009 10:43 AM

Thanks Haissam!

# Stu said on 27 August, 2009 12:44 PM

Any suggestions for Firefox 3.x?  This technique does not work there.

# ccc said on 28 August, 2009 07:22 AM

Thnkx .. Search 4 a long time for a solution. and finally found here !

# Sen said on 28 August, 2009 03:20 PM

Thanks Sam !!

# Graham Wood said on 03 September, 2009 11:47 AM

Just thought I'd add to the "Thank", took a while to find out how to do this, normally I'm working in the unix world ;)

# James said on 11 September, 2009 01:07 PM

is there a way to close window without msg in all version of IE, as well as Firefox and others?

# Naomi said on 15 September, 2009 01:00 PM

Thanks!!!!!!

# Graham said on 28 September, 2009 04:32 AM

Cheers, your a star

# Shankar From Tuty said on 13 October, 2009 03:02 AM

Thanks for your code..

It works fine..

Thanks once again

# Prashant said on 07 November, 2009 01:47 AM

My problem was solved once i stumbled upon your code. Thanks!

# Vinod said on 10 November, 2009 01:08 AM

Thanks...it helped fine

# Glenn Belardo said on 23 November, 2009 05:45 PM

Great! Thanks...

# Hn said on 14 December, 2009 02:01 PM

You are da man!  Great tips!  Worked like Sylvester Stallone in Rocky! :=)

# Bruce said on 16 December, 2009 03:21 PM

Still a beautiful solution ... thanks for the idea!

I trimmed it down to

<a href="BLOCKED SCRIPTwindow.open('', '_self'); window.close();">Close Window</a>

Cheers!

# Riaz said on 29 December, 2009 11:06 PM

any idea for Firefox 3.5.6 ?

# samnodes said on 30 December, 2009 03:33 AM

Very nice - worked a treat.

# Using ActiveX to launch desktop applications from SharePoint « Olav Aukan said on 31 August, 2010 03:48 PM

Pingback from  Using ActiveX to launch desktop applications from SharePoint « Olav Aukan

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP