3

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.

2
  • Not possible that _blank would not work Commented Jan 19, 2017 at 5:59
  • keep this in a function and use window.open Commented Jan 19, 2017 at 6:08

4 Answers 4

16

Why not use window.open .

<a onclick="window.open('website.com' + location.search)"><a/>
Sign up to request clarification or add additional context in comments.

4 Comments

this will open a new windows not tab
@JalpeshVadgama: It is not necessary. In modern browsers, window.open will open in a new tab rather than a popup. Plus it depends on browser settings
window.open('your url', '_blank'); , check stackoverflow.com/a/19852472/3256489
Worked like a charm, great
2

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>

Comments

0

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>`

Comments

0

For PHP users

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.