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.

By Enrico

My greatest passion is technology. I am interested in multiple fields and I have a lot of experience in software design and development. I started professional development when I was 6 years. Today I am a strong full-stack .NET developer (C#, Xamarin, Azure)

This site uses Akismet to reduce spam. Learn how your comment data is processed.