How can I make a HTML a href hyperlink open a new window?
Asked Answered
T

4

75

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?

Tarlatan answered 11/11, 2012 at 22:6 Comment(3)
Have you tried window.open("location", target) rather than window.location?Franglais
This worked @Franglais if you place a full answer I will mark it. ThanksTarlatan
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.Gabi
F
134
<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>
Franglais answered 11/11, 2012 at 22:12 Comment(5)
Which one should I use? Why? Thank youPreterite
@Preterite 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).Nantucket
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.Nantucket
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 variantsContradict
Opens in a new tab of the same browser window but not in a new window.Ambrosio
H
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>
Heartburn answered 14/5, 2021 at 9:47 Comment(0)
M
2

to open in a window.

<a href="http://www.google.com" onclick="window.open(this.href, 'new', 'popup'); return false;">Open in a window</a>
Messily answered 22/9, 2023 at 14:29 Comment(1)
Interesting answer as it tries to force a new window by way of pop-up. Does this work with pop-up blocking though?Ellis
W
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

Wether answered 28/4, 2021 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.