Html javascript to open new window and close current window

扶醉桌前 提交于 2019-11-28 13:26:30

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.

Samsky

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!