Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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". 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. I also can't make a javascript function because I have more than one link to open.

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.

like image 736
bigbryan Avatar asked Apr 07 '26 13:04

bigbryan


2 Answers

Why not use window.open .

<a onclick="window.open('website.com' + location.search)"><a/>
like image 182
RahulB Avatar answered Apr 09 '26 01:04

RahulB


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>
like image 21
marmeladze Avatar answered Apr 09 '26 03:04

marmeladze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!