JavaScript / jQuery - 在弹出窗口中打开当前链接

JavaScript / jQuery  - 在弹出窗口中打开当前链接

问题描述:

<a href="http://google.com">Link</a>

如何在弹出窗口中打开此链接?并阻止浏览器阻止它

How can I open this link in a pop-up window? And prevent the browser to block it

有新窗口,还有弹出窗口。使用 target = _blank 将在新窗口中打开,但默认情况下现代浏览器会将新窗口置于新的标签中。这听起来不像你想要的那样。

There's "new windows" and there's "popups". Using target=_blank will open in a new window, except that modern browsers put new windows in new tabs by default. Which sounds like it isn't what you want.

对于你想要的实际弹出窗口 window.open() ,并确保包含一些特定的宽度和高度,否则一些浏览器仍然会将新窗口放在一个新的选项卡中。 Darin的例子对我来说很好。

For an actual popup you want window.open(), and be sure to include some specific width and height, otherwise some browsers will still put the new window in a new tab. Darin's example looks good to me.

对于弹出窗口阻止,浏览器采用的一般方法是允许由用户操作启动的弹出窗口(例如点击),而通过脚本自发启动的弹出窗口,例如这个,被阻止:

As for popup blocking, the general approach that browsers take is that popups initiated by user action are allowed (such as clicking), while popups initiated spontaneously through script, such as this, are blocked:

<script type="text/javascript">
    window.open("http://www.google.com/", "Google", "width=500,height=500");
</script>

但是,广告拦截是一场不断升级的战争,你永远无法确定是否会打开一个弹出窗口。如果您的弹出窗口阻止,则window.open调用将返回null。所以我会像这样修改达人的例子:

However, ad blocking being an escalating war, you can never be sure that a popup will open. If your popup is blocked, the window.open call returns null. So I would modify Daren's example like this:

<a href="http://www.google.com/"
    onclick="return !window.open(this.href, 'Google', 'width=500,height=500')"
    target="_blank">

如果弹出窗口被阻止,则onclick返回 true ,在他们点击的链接之后,在新窗口或标签中打开它。这是一个后备,所以至少内容是可访问的(如果不是很漂亮)。

If the popup is blocked, onclick returns true, which follows the link they clicked by opening it in a new window or tab. It's a fallback, so at least the content is accessible (if not pretty).