使用 JavaScript 在新窗口中打开 URL

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

Open URL in new window with JavaScript

javascriptnew-window

提问by Mark Mitchell

I'm making a "share button" to share the current page. I would like to take the current page URL and open it in a new window. I have the current URL part working, but can't seem to get the next part working.

我正在制作一个“分享按钮”来分享当前页面。我想获取当前页面的 URL 并在新窗口中打开它。我有当前的 URL 部分工作,但似乎无法让下一部分工作。

I'm struggling with the syntax. I would like to specify the new window size to width=520, height=570.

我在语法上挣扎。我想将新窗口大小指定为width=520, height=570.

Something like:

就像是:

<a target="_blank"
   href="https://www.linkedin.com/cws/share?mini=true&amp;url=[sub]" 
   onclick="this.href = this.href.replace('[sub]',window.location)">
    LinkedIn
</a>

Any ideas?

有任何想法吗?

回答by citruspi

Use window.open():

使用window.open()

<a onclick="window.open(document.URL, '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');">
  Share Page
</a>

This will create a link titled Share Pagewhich opens the current url in a new window with a height of 570 and width of 520.

这将创建一个标题为Share Page在新窗口中打开当前 url 的链接,高度为 570,宽度为 520。

回答by Shiplu Mokaddim

Just use window.open()function? The third parameter lets you specify window size.

只用window.open()函数?第三个参数让您指定窗口大小。

Example

例子

var strWindowFeatures = "location=yes,height=570,width=520,scrollbars=yes,status=yes";
var URL = "https://www.linkedin.com/cws/share?mini=true&amp;url=" + location.href;
var win = window.open(URL, "_blank", strWindowFeatures);

回答by Ankit Kumar Rajpoot

Don't confuse, if you won't give any strWindowFeatures then it will open in a new tab.

不要混淆,如果您不提供任何 strWindowFeatures,那么它将在新选项卡中打开。

window.open('https://play.google.com/store/apps/details?id=com.drishya');