HTML button opening link in new tab

2020-02-09 06:11发布

So this is the simple code for the button to open a certain link

                <button class="btn btn-success" onclick="location.href='http://google.com';"> Google</button>

but it opens it on the same page, I want the link to open on a new tab though.

标签: html web
7条回答
看我几分像从前
2楼-- · 2020-02-09 06:46

Try this code.

<input type="button" value="Open Window"
onclick="window.open('http://www.google.com')">
查看更多
Melony?
3楼-- · 2020-02-09 06:47

Use '_blank'. It will not only open the link in a new tab but the state of the original webpage will also remain unaffected.

查看更多
何必那么认真
4楼-- · 2020-02-09 06:52

You can use the following.

window.open(
  'https:http://google.com',
  '_blank' // <- This is what makes it open in a new window.
);

in HTML

 <button class="btn btn-success" onclick=" window.open('http://google.com','_blank')"> Google</button>

plunkr

查看更多
神经病院院长
5楼-- · 2020-02-09 06:53

Try using below code:

<button title="button title" class="action primary tocart" onclick=" window.open('http://www.google.com', '_blank'); return false;">Google</button>

Here, the window.open with _blank as second argument of window.open function will open the link in new tab.

And by the use of return false we can remove/cancel the default behavior of the button like submit.

For more detail and live example, click here

查看更多
\"骚年 ilove
6楼-- · 2020-02-09 07:04

With Bootstrap you can use an anchor like a button.

<a class="btn btn-success" href="https://www.google.com" target="_blank">Google</a>

And use target="_blank" to open the link in a new tab.

查看更多
虎瘦雄心在
7楼-- · 2020-02-09 07:07

Try this

window.open(urlValue, "_system", "location=yes");
查看更多
登录 后发表回答