最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

window.open - Html javascript to open new window and close current window - Stack Overflow

programmeradmin0浏览0评论

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

<a href="" 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 .

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

Any ideas?

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?

Share Improve this question edited Jun 6, 2018 at 2:15 Andrew 20.1k13 gold badges108 silver badges121 bronze badges asked Sep 9, 2010 at 22:59 EatdokuEatdoku 6,94113 gold badges65 silver badges101 bronze badges 3
  • 1 This has been asked before: stackoverflow.com/questions/760422 – user123444555621 Commented Sep 9, 2010 at 23:13
  • 1 @Pumbaa80 - That's not what OP asked. That question is about why a window won't close. OP's window will close, but doesn't open a new one. – Ender Commented Sep 9, 2010 at 23:25
  • Right. Sorry, I didn't read thoroughly. – user123444555621 Commented Sep 10, 2010 at 6:55
Add a comment  | 

3 Answers 3

Reset to default 14

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.

The question is actually solved for the opener but it didn't help my issue (not wished: the new windows under firefox keep the same size as the current popup).

So I find following solution:

function whenClicked() 
{
    window.close();
    opener.location.href = "http://www.example.com";

}

or this if the ppage should open in a new tab:

function whenClicked() 
{
    window.close();
    opener.open(http://www.example.com, '_blank');
}

When you add some functionality to an element's click event via javascript, that functionality is executed before the default click event (in this case, opening a new page), in order to allow for the possibility of intercepting and overriding the default event. The default behavior will only execute when and if the event returns a boolean value of true.

In this case, the additional functionality would be to close the window and my guess is that Firefox chooses to interpret this as "we're all done here", so the click event never returns true, and thus the new page never gets opened.

Evgeny's suggestion of using a short timeout would allow the click event to return true before the window is closed, thus allowing the new window to open.

发布评论

评论列表(0)

  1. 暂无评论