JavaScript / jQuery - 在弹出窗口中打开当前链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3219325/
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
JavaScript / jQuery - Open current link in pop-up window
提问by Alex
<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
如何在弹出窗口中打开此链接?并防止浏览器阻止它
回答by jpsimons
There's "new windows" and there's "popups". Using target=_blankwill open in a new window, except that modern browsers put new windows in new tabsby default. Which sounds like it isn't what you want.
有“新窗口”和“弹出窗口”。使用target=_blank将在新窗口中打开,除了现代浏览器默认将新窗口放在新选项卡中。这听起来不是你想要的。
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.
对于您想要的实际弹出窗口,请window.open()确保包含一些特定的宽度和高度,否则某些浏览器仍会将新窗口放在新选项卡中。Darin 的例子对我来说看起来不错。
As for popup blocking, the general approach that browsers take is that popups initiated by user actionare 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>
However, ad blocking being an escalating war, you can never be sure that a popup will open. If your popup isblocked, the window.open call returns null. So I would modify Daren's example like this:
但是,广告拦截是一场不断升级的War,您永远无法确定是否会打开弹出窗口。如果您的弹出窗口被阻止,window.open 调用将返回 null。所以我会像这样修改 Daren 的例子:
<a href="http://www.google.com/"
onclick="return !window.open(this.href, 'Google', 'width=500,height=500')"
target="_blank">
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).
如果弹出窗口被阻止,则 onclick 返回true,通过在新窗口或选项卡中打开它来跟随他们单击的链接。这是一个后备,所以至少内容是可访问的(如果不漂亮的话)。
回答by Darin Dimitrov
<a href="http://google.com" onclick="window.open(this.href, 'windowName', 'width=1000, height=700, left=24, top=24, scrollbars, resizable'); return false;">Link</a>
回答by Codler
This will open a new window.
这将打开一个新窗口。
<a href="http://google.com" target="_blank">Link</a>
回答by Codler
jQuery:
jQuery:
<script>
$('#button2').live("click",function(e){
window.open("http://www.google.com", "yyyyy", "width=480,height=360,resizable=no,toolbar=no,menubar=no,location=no,status=no");
return false;
});
</script>
<a href="#" id="button2" ><img src="images/online.png"></a><br/>Online
回答by creativedirektor
You can try the code below,
你可以试试下面的代码
<script type="text/javascript">
window.open(location.href, "Google", "width=500,height=500");
</script>

