简体   繁体   English

如何使用 onclick="javascript:location.href" 打开新标签?

[英]How to open new tab using onclick="javascript:location.href"?

I'm trying to figure out if there's a way to open a new tab using " javascript:location.href ".我想弄清楚是否有办法使用“ javascript:location.href ”打开一个新选项卡。 I can't use other methods to open the link because it needs to get the ID of a certain member of my website when it loads.我无法使用其他方法打开链接,因为它在加载时需要获取我网站某个成员的 ID。 I also can't make a javascript function because I have more than one link to open.我也无法创建 javascript 函数,因为我要打开多个链接。

target="_blank" //didn't work

Here's a piece of my code:这是我的一段代码:

<a onclick="javascript:location.href='website.com' + location.search"><a/>

If there are other ways to do this, please let me know.如果有其他方法可以做到这一点,请告诉我。

为什么不使用window.open。

<a onclick="window.open('website.com' + location.search)"><a/>

ofcourse your scenario is not that simple i've written below, but have a look up my snippet. 当然,您的情况不是我在下面写的那么简单,但请查看我的代码段。 it dynamically extracts a value and appends it to anchor. 它动态提取一个值并将其附加到锚点。 which will be opened in new tab. 它将在新标签页中打开。

 var id = document.getElementById('id').innerHTML document.getElementById('pseudo-dynamism').href += '?id='+ id 
  <div id="main"> <div id="member-details" class="question" data-answered="False"> <span id="id">23</span> </div> <hr> <a href="http://example.com" id="pseudo-dynamism" target="_blank">Click</a> </div> 

use this It will open new window. 使用它会打开新窗口。

 `<button 
onclick="window.open
(this.href,'popUpWindow','height=600,width=600,left=10,top=10,,
scrollbars=yes,menubar=no'); 
return false;" >
new window
</button>`

For PHP users对于 PHP 用户

$url = 'stackoverflow.com';
<a onclick="window.open(<?php echo $url ?> + location.search)">Website<a/>

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

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