Proxy > Gmail Facebook Yahoo!

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



If you've ever tried to close a browser window from ASP.NET by using the following code:

<input type="button" class="inputfields"
 onclick="javascript:window.close();" value="Close" /></td>
You will probably receive the following message:

After digging around for 20 minutes or so, I finally found the answer on Anatoly Lubarsky's blog:

<input type="button" class="inputfields"
onclick="javascript:window.opener='x';window.close();" value="Close" /></td>

 
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.

For Mozilla Firefox

set your firefox browser:
1. input "about:config " to your firefox address bar and enter;
2. make sure your "dom.allow_scripts_to_close_windows" is true



Responses

0 Respones to "JavaScript: How to Close Browser Window (window.close()) Without Warning"


Send mail to your Friends.  

Expert Feed

 
Return to top of page Copyright © 2011 | My Code Logic Designed by Suneel Kumar