代码之家  ›  专栏  ›  技术社区  ›  Alex

javascript/jquery-在弹出窗口中打开当前链接

  •  17
  • Alex  · 技术社区  · 15 年前
    <a href="http://google.com">Link</a>
    

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

    5 回复  |  直到 9 年前
        1
  •  63
  •   jpsimons    15 年前

    有“新窗口”和“弹出窗口”。使用 target=_blank 将在新窗口中打开,除非现代浏览器将新窗口置于新窗口中 制表符 默认情况下。听起来这不是你想要的。

    对于您想要的实际弹出窗口 window.open() ,并确保包含一些特定的宽度和高度,否则某些浏览器仍会将新窗口置于新选项卡中。我觉得达林的例子不错。

    至于弹出窗口阻止,浏览器采用的一般方法是 用户行为 允许(如单击),而通过脚本自动启动的弹出窗口(如此)被阻止:

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

    然而,广告拦截是一场不断升级的战争,你永远无法确定弹出窗口是否会打开。如果你弹出 被阻止,window.open调用返回空值。因此,我将修改Daren的示例,如下所示:

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

    如果弹出窗口被阻止,onclick返回 true 在他们单击的链接之后,在新窗口或选项卡中打开该链接。这是一个回退,所以至少内容是可以访问的(如果不是漂亮的话)。

        2
  •  9
  •   Darin Dimitrov    15 年前
    <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>
    
        3
  •  2
  •   Codler    15 年前

    这将打开一个新窗口。

    <a href="http://google.com" target="_blank">Link</a>
    
        4
  •  1
  •   YumYumYum Paul Campbell    12 年前

    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
    
        5
  •  -1
  •   Vinoth Krishnan    9 年前

    您可以尝试下面的代码,

    <script type="text/javascript">
         window.open(location.href, "Google", "width=500,height=500");
    </script>