Javascript Html javascript打开新窗口并关闭当前窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3681178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:42:01  来源:igfitidea点击:

Html javascript to open new window and close current window

javascriptwindow.open

提问by Eatdoku

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.

目的是在用户单击图像链接时关闭此弹出窗口,并打开一个新页面并定向到http://www.example.com

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

它适用于 IE 和 Chrome,但不适用于 Firefox。弹出窗口关闭,但没有打开新窗口。

Any ideas?

有任何想法吗?

回答by EMP

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

是的,我可以复制这个 - 很有趣。setTimeout解决它:

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.

我只能猜测,一旦窗口关闭(发生在超链接之前),Firefox 就会停止处理该页面。

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

编辑:最好让它延迟 10 毫秒 - 1 毫秒 Chrome 不会关闭窗口。

回答by 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).

这个问题实际上已针对 opener 解决了,但对我的问题没有帮助(不希望:firefox 下的新窗口与当前弹出窗口的大小保持相同)。

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');
}

回答by Ender

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.

当您通过 javascript 向元素的点击事件添加一些功能时,该功能会在默认点击事件(在本例中,打开一个新页面)之前执行,以允许拦截和覆盖默认事件的可能性。默认行为只会在事件返回布尔值 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.

在这种情况下,附加功能是关闭窗口,我的猜测是 Firefox 选择将其解释为“我们都在这里完成了”,因此单击事件永远不会返回 true,因此新页面永远不会打开。

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.

Evgeny 使用短超时的建议将允许单击事件在窗口关闭之前返回 true,从而允许打开新窗口。