Html javascript to open new window and close current window

Eatdoku picture Eatdoku · Sep 10, 2010 · Viewed 46.3k times · Source

I have a popup window and in that page I have the following code in the body.

<a href="http://www.example.com" target="_blank" onClick="javascript:window.close()"><img src="...something"/></a>

The purpose is to have this popup window close when a user clicks on the image link, and to open a new page and be directed to http://www.example.com.

It works in IE and Chrome, but not in Firefox. The popup window closes but no new window is opened.

Any ideas?

Answer

EMP picture EMP · Sep 10, 2010

Yes, I can repro this - interesting. setTimeout works around it:

onClick="javascript: setTimeout(window.close, 10);"

I can only guess that once the window closes (which happens before the hyperlink is followed) Firefox stops processing that page.

Edit: better make it 10ms delay - with 1ms Chrome doesn't close the window.