Html 使用 Javascript 在弹出窗口中打开链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16182915/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:49:26  来源:igfitidea点击:

Open link in Popup Window with Javascript

javascripthtmlhyperlinkpopupmodal-dialog

提问by user2105885

I'm trying to load a href into a popup window of specific dimensions which also centers in the screen.

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

Here is my source:

这是我的来源:

<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>

And here is my javascript:

这是我的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");
}

This seems to return a 404 error when tested. What am i doing wrong?

测试时,这似乎会返回 404 错误。我究竟做错了什么?

Thanks heaps.

谢谢堆。

回答by Mal

function windowpop(url, width, height)The function requires a URL to be returned to it.

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

onclick="return windowpop(545, 433)"You're only returning the widthand height.

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

Try returning the URL using this.href:

尝试使用this.href以下方法返回 URL :

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

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

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