target="_blank" vs. target="_new"
Asked Answered
C

11

548

What's the difference between <a target="_new"> and <a target="_blank"> and which should I use if I just want to open a link in a new tab/window?

Chalutz answered 10/2, 2011 at 23:54 Comment(4)
Summary of current anwsers: _new doesn't have any special meaning. You could write _white_little_lamb as well.Hexangular
@x3ro How does that make it wrong to say that "_new" doesn't have any special meaning?Hexangular
@ÁlvaroG.Vicario I was referring to "You could write ... instead". But thinking about it, I'd argue that "has no special meaning" is not the same as "is explicitly discouraged", but that might be me nitpicking ;)Malevolent
I would like to remind everyone that using target="_blank" without rel="noopener" is a potential security vulnerability. Search for rel="noopener" to learn more.Celik
M
689

Use "_blank"

According to the HTML5 Spec:

A valid browsing context name is any string with at least one character that does not start with a U+005F LOW LINE character. (Names starting with an underscore are reserved for special keywords.)

A valid browsing context name or keyword is any string that is either a valid browsing context name or that is an ASCII case-insensitive match for one of: _blank, _self, _parent, or _top." - Source

That means that there is no such keyword as _new in HTML5, and not in HTML4 (and consequently XHTML) either. That means, that there will be no consistent behavior whatsoever if you use this as a value for the target attribute.

Security recommendation

As Daniel and Michael have pointed out in the comments, when using target _blank pointing to an untrusted website, you should, in addition, set rel="noopener". This prevents the opening site to mess with the opener via JavaScript. See this post for more information.

Malevolent answered 11/2, 2011 at 0:7 Comment(14)
Leaving out the bolded text from the spec makes that quote sound confusingly circular. I read it as "A valid browsing context name or keyword is any string that is either a valid browsing context name or ...."Chalutz
Guess you're right. But my understanding is, that browsing context name refers to frame names, etc.Malevolent
@lyoshenka, @x3ro: I took the liberty to add the previous paragraph to the quote. That should clear up the confusion.Claresta
From the HTML 4 link: «Except for the reserved names listed below, frame target names [...] must begin with an alphabetic character (a-zA-Z). User agents should ignore all other target names.»—I don't know if they do that in practice. (The HTML 5 spec doesn't seem to provide explicit instructions for invalid names.)Hexangular
@qarma Do you know of a resource that sums up the behaviour of popular browsers?Malevolent
I wish. I searched and failed. AFAIK desktop browsers support it, I can't vouch for Android/iOS/etc.Aardwolf
"_new" means "name that window _new and load content", where "_blank" means "create a new window each time and load content". So, if you use "_blank" you get multiple windows, qtty of clicks = qtty of windows. I have a case where I wanted to load different contents in a new window, if I use "_blank" I get a new window each time I click a link with "_blank" target, if I use a custom name "my_custom_name", each click in a link it will load in the same window, named "my_custom_name", no repeated windows. So, "_new" or "_whatever" are valid too. See jsfiddle.net/ALMH6/1Winter
@aesede: Read the spec. They are not valid, because "my_custom_name" may not be prefixed with an underscore. Thus "new" would be fine, but "_new" is not!Malevolent
@x3ro I agree on that, but as on many cases in HTML there are "invalid" things that the browsers tend to allow, like this case. Whoever had the idea of using for the first time "_new" maybe thought that using an underscore would follow the logic of the reserved words (_blank, _parent, etc) as it tries to have a semantic use: _new means "this is not just a named target for a frame, nor a reserved functionality, this means open a new, unique window". Again, I agree the underscore is invalid.Winter
Securitywise this is an important read mathiasbynens.github.io/rel-noopenerTammy
There is no ! in the end "_blank"... ☺Kutenai
Please consider adding that people should also add rel="noopener noreferrer" due to javascript attack vulnerability of target="_blank"Piggish
those ancient nonstandard bits seem to stick forever. this is another mistake that can force a html5 page into compatibility mode in IE. I just fixed it in one of my projects, had 17 occurrences, and I strongly recommend to use full text search and a service like validator.w3.org to get rid of those vestiges from the previous century. where can I upvote your username?Piecework
Per the linked post - "2021 update: Browsers now implicitly set rel="noopener" for any target="_blank" link, following a spec change."Extortion
O
139

Using target="_blank" will instruct the browser to create a new browser tab or window when the user clicks on the link.

Using target="_new" is technically invalid according to the specifications, but as far as I know every browser will behave the same way:

  • it will search for a tab or window with the context name "_new"
  • if a "_new" tab/window is found, then the URL is loaded into it
  • if it's not found, a new tab/window is created with the context name "_new", and the URL loaded into it

Note target="_new" will behave exactly the same as target="new", and the latter is valid HTML while the former is invalid HTML.

Adding some confusion to this, in HTML4 the target attribute was deprecated. In HTML5 this decision was reversed, and it is an official part of the spec once again. All browsers support target no matter what version of HTML you are using, but some validators will flag the use as deprecated if your doctype is HTML4.

Obsequious answered 15/1, 2012 at 2:18 Comment(2)
So, basically, if I have two (or more) links with target="_new", they both will open in the same tab, one overwriting the other?Heloise
@Heloise as far as I know, yes that is what will happen in every browser. However "_new" is an illegal value for a target so don't do it.Obsequious
I
27

I know this is an old question and the correct answer, use _blank, has been mentioned several times, but using <a target="somesite.com" target="_blank">Link</a> is a security risk.

It is recommended (performance benefits) to use:

<a href="somesite.com" target="_blank" rel="noopener noreferrer">Link</a>
Incapacity answered 27/8, 2017 at 6:50 Comment(1)
Just some additional reading on the security risk aspect: jitbit.com/alexblog/…Propagate
N
15

This may have been asked before but:

"every link that specifies target="_new" looks for and finds that window by name, and opens in it.

If you use target="_blank," a brand new window will be created each time, on top of the current window."

from here: http://thedesignspace.net/MT2archives/000316.html

Namangan answered 10/2, 2011 at 23:59 Comment(0)
B
14

it's my understanding that target = whatever will look for a frame/window with that name. If not found, it will open up a new window with that name. If whatever == "_new", it will appear just as if you used _blank except.....

Using one of the reserved target names will bypass the "looking" phase. So, target = "_blank" on a dozen links will open up a dozen blank windows, but target = whatever on a dozen links will only open up one window. target = "_new" on a dozen links may give inconstant behavior. I haven't tried it on several browsers, but should only open up one window.

At least this is how I interpret the rules.

Bloke answered 25/1, 2013 at 19:56 Comment(0)
E
13

target="_blank" opens a new tab in most browsers.

Ebner answered 10/2, 2011 at 23:54 Comment(0)
I
11

Caution - remember to always include the "quotes" - at least on Chrome, target=_blank (no quotes) is NOT THE SAME as target="_blank" (with quotes).

The latter opens each link in a new tab/window. The former (missing quotes) opens the first link you click in one new tab/window, then overwrites that same tab/window with each subsequent link you click (that's named also with the missing quotes).

Impendent answered 26/5, 2014 at 8:13 Comment(3)
Very good point. I ran into this issue when I didn't enclose the _blank in quotes. The link I clicked on from my newly created "blank" tab was over writing content within that tab instead of opening a new tab. Enclosing the _blank in quotes forces chrome to open a new tab every time.Disvalue
Also, make sure to use "_blank" not "_BLANK". Chrome is actually fussy about case and won't open a new tab using "_BLANK" multiple times.Disvalue
Not sure if this post is still relevant (I use FF, so I can't check), but if still true, this is a violation of the spec for unquoted attribute values; Essentially, anything that isn't a greater-than sign or whitespace is an acceptable unquoted attribute value character (some may produce parse errors, however).Tweedy
P
5
  • _blank as a target value will spawn a new window every time,
  • _new will only spawn one new window.

Also, every link clicked with a target value of _new will replace the page loaded in the previously spawned window.

You can click here When to use _blank or _new to try it out for yourself.

Persons answered 23/10, 2013 at 14:13 Comment(3)
Can you list which browsers honour _new as described?Aardwolf
@qarma That behaviour is as described in all the browsers I know of, _new isn't a magical key word, it's just a name, if a window with that name exists it'll reuse it, otherwise it'll open it. Clicking multiple links for that window will just open different pages in the named window, rather than opening multiple new pages.Harmattan
The spec actually suggests to browsers that they ignore targets that starts with underline but are not keywords. There are no suggestions for what a browser should do when it "ignores" an invalid target name. Possibilities include: (1) treat it like "_blank" (2) treat it like a window name (as if there wasn't an illegal underscore) (3) treat it like a explicitly empty window name (4) treat it like there was no target attribute. -- Any browser could pick any of the interpretations.Goodspeed
E
0

The target attribute of a link forces the browser to open the destination page in a new browser window. Using _blank as a target value will spawn a new window every time while using _new will only spawn one new window and every link clicked with a target value of _new will replace the page loaded in the previously spawned window

Expeller answered 3/8, 2012 at 21:41 Comment(0)
T
0

In order to open a link in a new tab/window you'll use <a target="_blank">.

value _blank = targeted browsing context: a new one: tab or window depending on your browsing settings

value _new = not valid; no such value in HTML5 for target attribute on a element

target attribute with all its values on a element: video demo

Towery answered 31/1, 2014 at 14:58 Comment(0)
S
-2

The use of _New is useful when working on pages that are Iframed. Since target="_blank" doesn't do the trick and opens the page on the same iframe... target new is the best solution for Iframe Pages. Just my five cents.

Silicone answered 17/5, 2016 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.