问题 使用Javascript在弹出窗口中打开链接[关闭]


我正在尝试将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错误。我究竟做错了什么?

谢谢堆。


12118
2018-04-24 03:34


起源

你还没发过 url 参数 windowpop 功能。 - Danny Beckett
function windowpop(url, width, height) 该函数期望您返回一个URL,但您只返回宽度和高度。 - Mal
这可能是有充分理由关闭的,但我觉得这很有帮助。我不知道window.open的任何额外参数。 - jstaab


答案:


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

onclick="return windowpop(545, 433)" 你只是回来了 width 和 height

尝试使用返回URL this.href

onclick="return windowpop(this.href, 545, 433)"

例:  http://jsfiddle.net/zKKAM/1/


12
2018-04-24 03:48



只是注意,我正在使用这种方法,如果您不允许在浏览器上弹出窗口,则工作正常,但是,如果您这样做,它将打开弹出窗口两次。我通过添加event.preventDefault()解决了这个问题。单击链接,并从windowpop()的开头删除返回。 - madagalbiati