72

This is my code:

<a href="http://www.google.com" onClick="window.location.href='http://www.yahoo.com';return false;" target="_blank">test</a>

When you click it, it takes you to Yahoo but it does not open a new window?

ADyson
  • 57,178
  • 14
  • 51
  • 63
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
  • 4
    Have you tried window.open("location", target) rather than window.location? – John Nov 11 '12 at 22:09
  • This worked @John if you place a full answer I will mark it. Thanks – TheBlackBenzKid Nov 11 '12 at 22:11
  • 1
    I sometimes miss the close happy days. I always get a chuckle when a question with 26k+ views and 5 votes (I was happy to be magic number 5 btw) was closed for not being real yo. Keep it real. –  Jan 14 '14 at 21:11

3 Answers3

130
<a href="#" onClick="window.open('http://www.yahoo.com', '_blank')">test</a>

Easy as that.

Or without JS

<a href="http://yahoo.com" target="_blank">test</a>
mgilson
  • 300,191
  • 65
  • 633
  • 696
John
  • 2,675
  • 2
  • 18
  • 19
  • 1
    Which one should I use? Why? Thank you – Cyborg Apr 10 '14 at 01:28
  • 5
    @Cyborg the second form (without JS) is the recommended one: a plain link _suggesting_ a new tab/window. The first example (with JS _onclick_) should only be used when the other option can't be used _because of a very specific technical reason_ (for example: if some component of a js/css framework/library recommends you to do so for a specific situation). – Roimer May 02 '14 at 01:06
  • 1
    Of course, if you opt for using Javascript then you probably should change the link by a button, and use `addEventListener` instead of inlining JS in the HTML. – Roimer May 02 '14 at 01:10
  • I would always try to use the second one as a user can see what he‘s opening exactly (your_url/# is not helpful) and I often use mouse wheel click to open a link in a new tab. The target takes care of this for me but I as a user don‘t check that before, so I just make Ctrl+Click or mouse wheel click to open it in a background tab. Interestingly I thought Ctrl+Click and mouse wheel click does the same thing but I learned that it does not. The 1st version works with Ctrl+Click and opens the correct path but mouse wheel click just opens url/# in the new tab. 2nd version works with both variants – bugybunny Oct 25 '18 at 09:25
5

In order to open a link in a new window, add Javascript command

onclick="window.open('text-link.htm', 'name','width=600,height=400') inside the <a> tag:

<a href="../html-link.htm" target="popup" onclick="window.open('../html-link.htm','name','width=600,height=400')">Open page in new window</a>
Anonymous Girl
  • 582
  • 8
  • 22
1

Given answer might be helpful when you want to open a new tab or browser user preference is set to open new window instead of tab, since the original question is about open new window not a tab, here is what works for me.

<a href="#" onClick='window.open(url, "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=800, height=600);'>test</a> 

Check this one too

Uttam Ughareja
  • 842
  • 2
  • 12
  • 21