【问题标题】:Open link in Popup Window with Javascript [closed]使用 Javascript 在弹出窗口中打开链接 [关闭]
【发布时间】:2013-04-24 03:34:42
【问题描述】:

我正在尝试将 href 加载到特定尺寸的弹出窗口中,该窗口也位于屏幕中心。

这是我的来源:

<li>
    <a class="sprite_stumbleupon" href="http://www.stumbleupon.com/submit?url=http://www.your_web_page_url" target="_blank" onclick="return windowpop(545, 433)"></a>
</li>

这是我的 javascript:

function windowpop(url, width, height) {
    var leftPosition, topPosition;
    //Allow for borders.
    leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
    //Allow for title and status bars.
    topPosition = (window.screen.height / 2) - ((height / 2) + 50);
    //Open the window.
    window.open(url, "Window2", "status=no,height=" + height + ",width=" + width + ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
}

这似乎在测试时返回 404 错误。我做错了什么?

非常感谢。

【问题讨论】:

  • 您尚未将url 参数发送到windowpop 函数。
  • function windowpop(url, width, height) 该函数希望您返回一个 URL,但您只返回宽度和高度。
  • 这可能有充分的理由关闭,但我发现它非常有用。我不知道 window.open 的任何这些额外参数。

标签: javascript html hyperlink popup modal-dialog


【解决方案1】:

function windowpop(url, width, height)该函数需要返回一个URL。

onclick="return windowpop(545, 433)" 你只返回widthheight

尝试使用this.href返回网址:

onclick="return windowpop(this.href, 545, 433)"
<script>
function windowpop(url, width, height) {
    var left = (screen.width / 2) - (width / 2);
    var top = (screen.height / 2) - (height / 2);
    window.open(url, "", "menubar=no,toolbar=no,status=no,width=" + width + ",height=" + height + ",top=" + top + ",left=" + left);
}
</script>

【讨论】:

  • 请注意,我使用的是这种方法,如果您不允许浏览器上的弹出窗口,它可以正常工作,但是,如果您这样做,它将打开两次弹出窗口。我通过添加 event.preventDefault(); 解决了它单击链接,并从 windowpop() 的开头删除返回。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
相关资源
最近更新 更多