简体   繁体   English

在新标签页中打开链接的按钮

[英]Button to open link in new tab

I am making a website and I want a new tab to open at a specific address when I click a button. 我正在建立一个网站,我希望在单击按钮时在特定地址打开一个新选项卡。

This is the html for my button: 这是我的按钮的html:

<button id="AsDownload" onclick="AsDownload();">Download</button>

And this is my javascript: 这是我的javascript:

function AsDownload(){
    chrome.tabs.create({url:"https://alexanderhawking.itch.io/asteroid-racer"});
};

But it is not working and I can't figure out why, can someone help me? 但这不起作用,我不知道为什么,有人可以帮助我吗?

Did you try window.open() ? 您是否尝试过window.open() It opens URL in new tab. 它将在新选项卡中打开URL。

如果仍然希望将其用作按钮而不是锚标记<a> ,请在函数中使用以下代码:

window.open("https://alexanderhawking.itch.io/asteroid-racer");

you can integrate a link and a button like that 您可以像这样集成链接和按钮

  <button id="AsDownload"><a href="https://alexanderhawking.itch.io/asteroid-racer/"target="_blank">load</a></button>

this would open up in a new tab 这会在新标签页中打开

In addition to the other answers, add "_blank" to open in a new tab if you intend on using a JavaScript function. 除了其他答案,如果您打算使用JavaScript函数,请添加“ _blank”以在新标签中打开。

 function AsDownload() { //window.open(pathString, target); window.open("https://alexanderhawking.itch.io/asteroid-racer", "_blank"); }; 
 <button id="AsDownload" onclick="AsDownload()">Download</button> 

You can use window.open() to do that. 您可以使用window.open()来做到这一点。

<input type="button" value="button name" onclick="window.open('http://www.website.com')" />

Your example: 你的例子:

function AsDownload() {
  window.open("https://alexanderhawking.itch.io/asteroid-racer", "_blank");
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM