07 January, 2010

How to close a Browser window without warning

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:

Anonymous said...

Way to go!
Pretty easy and straigtforward...

Hugo Petitpas said...

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

Claus Konrad said...

Nice to know it also helped you out ;-)

Juan Carlos said...

great!! i have the same problem and solution 2 works for me

Ben said...

Thanks! All I could find were the non-working examples. ;)

Anonymous said...

You da man! Thanks!

Raj said...

Good solution, we have a IE6 and IE8 and had this solution working us with the existing IE6 code.

Great Job!!!

Pedro said...

Nice work - saved a lot of trouble!

Rob said...

Very nice. I saw this code elsewhere but blew it off. Your explanation keyed me into IE8.

Anonymous said...

Wow - all other discussion did not work - your nailed it!!! Thanks!!!

othalis said...

Nice one, thanks for the solution!

ignasi said...

tanks alot!!!!

bugs said...

After Searching a lot many codes......finally urs, it worked!!! Thank you...

Philippe said...

Thenk you. Works perfectly.

Anonymous said...

Thanks for the Solution.

But it works for me when i have Java 1.6..But if i have Java 1.5,, it fails...

Jason said...

The second solution you posted DEFINITELY works...Thanks man, perfect solution, exactly what I was looking for. This works for IE9 as well.

Unknown said...

thank u very much.but it works for only IE not for Other Browsers

Allen Conway said...

Excellent! Worked in Chrome for me as well.

Emiliano Bartoloni said...

Many Thanks , it worked in Ie8

taluk said...

Great stuff !

Steve said...

This doesnt work in IE10, any ideas on how this could be achieve in 1E10?

Claus Konrad said...

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)

bizzare said...

Thanks it works me also and tutorial is very helpful

Anonymous said...

I tried whole bunch of things on the internet nothing worked. But your solution did work! Thanks a lot!

Anonymous said...

Thank you , Its working for me

Abhik Dey said...

Awesome Man

anishiva said...

Great man

InRiver: Not loading your extensions?

(You really need to in the loop to appreciate the issue this post addresses). Man, I've been fighting this problem for hours before I ...