I’m not really a web developer, but today a friend asked me how to force close a browser window? And also without the infamous security measure (“Are you sure you want to close?”) taken by Microsoft in IE 7 and forward.
It goes without saying, that it is a local operation on the Client-side as we are talking about closing his browser. It can therefore not happen server side (really..?). The only option we are left with is to execute some script in the browser, or to let the Client download a browser plug-in that forces the browser to close. Well – I took the easy path and went for the scripting option.
According to Google, I’m not the only one with this problem! A number of other dudes have faced the same problem. The recipe (1) they used proved not to work in my case. I found only one solution that actually worked (2).
The trick is to let the browser believe that the browser window has been opened by JavaScript in the first place. If the browser believes, that the window is the initial window (window.opener = null), it will ask for a confirmation from the User. If the browser on the other hand believes, the window has been opened by JavaScript (and thereby carrying an '.opener'), it will close without prompting. This should actually make option (1) work as you explicitly set the owner, but for some reason unknown to me, it did not work at all in my IE8 browser. Option (2) works like a charm instead!
(1) This does not work for me. You are still present with a dialog using this option (IE 8).
function closeWin() {
window.opener = ''x'';
window.close();
}
(2) This WORKS!
function closeWin() {
window.open('', '_self', '');
window.close();
}
Invoking this last function from a client side button (html button), will close the browser without asking any questions or presenting any warnings. I suspect the reason is that you open the windows “into itself” so to speak. The current window has therefore been opened by JavaScript, which allows IE to close the window without prompts.
Neat work, but still somewhat bizarre workaround :-)
27 comments:
Way to go!
Pretty easy and straigtforward...
Thanks, you saved my day, it worked for me too.
The only i want to add: for me, changing the window.opener to the open('', '_self') made it stop working in IE6. I left the window.opener AND added the window.open, and everything works in IE6 and IE8
Nice to know it also helped you out ;-)
great!! i have the same problem and solution 2 works for me
Thanks! All I could find were the non-working examples. ;)
You da man! Thanks!
Good solution, we have a IE6 and IE8 and had this solution working us with the existing IE6 code.
Great Job!!!
Nice work - saved a lot of trouble!
Very nice. I saw this code elsewhere but blew it off. Your explanation keyed me into IE8.
Wow - all other discussion did not work - your nailed it!!! Thanks!!!
Nice one, thanks for the solution!
tanks alot!!!!
After Searching a lot many codes......finally urs, it worked!!! Thank you...
Thenk you. Works perfectly.
Thanks for the Solution.
But it works for me when i have Java 1.6..But if i have Java 1.5,, it fails...
The second solution you posted DEFINITELY works...Thanks man, perfect solution, exactly what I was looking for. This works for IE9 as well.
thank u very much.but it works for only IE not for Other Browsers
Excellent! Worked in Chrome for me as well.
Many Thanks , it worked in Ie8
Great stuff !
This doesnt work in IE10, any ideas on how this could be achieve in 1E10?
I just tested this with IE 10 and can not make it stop working? It closes the windows when pressed.
Try copy/paste the script into your html page. Note that you need to allow activeX/script to run in the page (IE will potentially prompt you when you render the page the first time)
Thanks it works me also and tutorial is very helpful
I tried whole bunch of things on the internet nothing worked. But your solution did work! Thanks a lot!
Thank you , Its working for me
Awesome Man
Great man
Post a comment