171

I recall reading somewhere that in HTML5 it was no longer okay to use target="_blank" in HTML5, but I can't find it now.

Is it alright to continue to use target="_blank"?

I know it's generally considered a bad idea, but it's by the easiest way to open a new window for something like a PDF, and it also doesn't require you to rely on JavaScript.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
  • 1
    If it's a bad idea, then what's the good idea? `javascript: window.open(...)`? – hobbes3 May 09 '12 at 07:34
  • 2
    I've heard many people complain about opening new windows, no matter the method. It has a number of problems, including confusing the user and messing up the history. I think that's why I said "it's generally a bad idea" although I use it all over the place. – Darryl Hein May 10 '12 at 03:55
  • 7
    Well generally if you want to open an external site from your site, then opening a new window (which is probably a new tab on most browser's these day) is a better idea in my opinion. – hobbes3 May 10 '12 at 07:39
  • 3
    it's okay and not deprecated anymore: http://dev.w3.org/html5/markup/a.html – timaschew Sep 29 '12 at 12:53
  • I too have heard it's a "bad idea" by those who believe that where a link opens should be up to the user, not the author. Can anyone direct me to any opinions about the subject? I've read the specs but I'd like to see how people interpret them. – chharvey Oct 06 '12 at 19:26
  • 7
    @hobbes3, please stop messing my navigation and my history. I'm the one who's browsing, and it's me who's deciding if a link should open in a new (middle-click) or in the same (left-click) tab. When I come across a web-site forcing me to follow its "rightful best-practice" opening every link in new tabs, I'll promptly and gladly leave. – Albireo Jan 24 '13 at 07:23
  • 12
    @Albireo, It's just my opinion and some popular web services incorporate the same idea as well. For example, clicking on a link in your mail in Gmail opens a new window by default. – hobbes3 Feb 02 '13 at 21:47
  • 15
    @Albireo you're assuming the user is always as advanced and tech-savvy as you are. – Mike Campbell Jan 31 '14 at 14:13
  • 3
    There's a reason why `target` et al. exist. Sometimes they're a necessity. A dynamically generated page with stateful JS, for example, where an iframe is less desirable than a new tab (e.g. viewing a PDF, as the OP states). Or when a secure site (such as a bank) sends you to an external link but does not allow the use of the back button due to content expiration. Like all things this HTML attribute is not a "bad idea" to use, but it should be used with thought and care, as with every library, class, function and property in all of programming. – s.co.tt Dec 19 '14 at 17:10
  • @Albireo Why would the navigation be yours? Nothing else on the page you visit is. – Joeri Apr 25 '22 at 09:56

9 Answers9

164

It looks like target="_blank" is still alright. It is listed as a browsing context keyword in the latest HTML5 draft.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
mike
  • 4,393
  • 1
  • 27
  • 16
  • 57
    `target="_blank"` will cause a new window to open *every time* the user clicks the link. Unless this is really what you want to happen (and it rarely is) consider using `target="somethingUnique"` so that the user only gets the one window opening, even if they click the link several times. It makes for a much nicer UX. – BanksySan Aug 02 '13 at 09:27
  • 4
    @BanksySan: One example where I think that `target="_blank"` is good are sharing buttons. – Martin Thoma Jan 05 '14 at 14:11
  • 1
    If I'm reading a long article and there's a reference to some related information on another site (or the same site). I often want to jump back and forth between the two without losing my place in the original article. target="_blank" is perfect for this. Leaving the page and having to alternately reload each page (often losing my place in either page), is not. Yes, I know that I can right-click on the link and open it in a new tab, but I don't want to because it will break my concentration, and I suspect that the majority of web users don't know how. – Bob Ray May 20 '17 at 20:08
  • The target attribute has many benefits. Im not gonna disq that. The popup advertisers destroyed its purpose, but today with so many services running aside the browsing experiance, many people dont wanna leave the current article or tab, but still interact with the heavy download PDF or video. Using _blank make shure that you let the user populate an empty tab - not override the current populating content, if you serve many links on the same session/ site. Unique names are just messy in an audio-list with 90 songs.... Viva la target, viva la _blank... – Jonas Lundman Jun 06 '18 at 23:17
93

It is ok to use target="_blank"; This was done away with in XHTML because targeting new windows will always bring up the pop-up alert in most browsers. XHTML will always show an error with the target attribute in a validate.

HTML 5 brought it back because we still use it. It's our friend and we can't let go.

Never let go.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Graham
  • 931
  • 6
  • 2
17

Though the target="_blank" is acceptable in HTML5, I personally try never to use it (even for opening PDFs in a new window).

HTML should define meaning and content. Ask yourself, “would the meaning of the a element change if the target attribute were removed?” If not, the code should not go in the HTML. (Actually I’m surprised the W3C kept it… I guess they really just can’t let go.)

Browser behavior, specifically, interactive behavior with the user, should be implemented with client-side scripting languages like JavaScript. Since you want the browser to behave in a particular way, i.e., opening a new window, you should use JS. But as you mentioned, this behavior requires the browser to rely on JS. (Though if your site degrades gracefully, or enhances progressively, or whatever, then it should still be okay. The users with JS disabled won’t miss much.)

That being said, neither of these is the right answer. Out there somewhere is the opinion that how a link opens should ultimately be decided by the end user. Take this example.

You’re surfing Wikipedia, getting deeper and deeper into a rabbit hole. You come across a link in your reading.

Let’s say you want to skim the linked page real quick before coming back. You might open it in a new tab, and then close it when you’re done (because hitting the ‘back’ button and waiting for page reload takes too long). Or, what if it looks interesting and you want to save it for later? Maybe you should open it in a new background tab instead, and keep reading the current page. Or, maybe you decide you’re done reading this page, so you’ll just follow the link in the current tab.

The point is, you have your own workflow, and you’d like your browser to behave accordingly. You might get pretty frustrated if it made these kinds of decisions for you.

THAT being said, web developers should make it absolutely clear where their links go, what types and/or formats of sources they reference, and what they do. Tooltips can be your friend (unless you're using a tablet or phone; in that case, specify these on the mobile site). We all know how much it sucks to be taken somewhere we weren't expecting or make something happen we didn't mean to.

Community
  • 1
  • 1
chharvey
  • 8,580
  • 9
  • 56
  • 95
  • And if anyone was clever enough to design a user widget that allowed one to very easily exercise their God-given right of control with every link (easily, as in subconsciously), then it should be added to browsers and all tags could provide it. – Mark Goldfain Mar 01 '14 at 10:57
  • This answer is logically and architecturally the most accurate, in my opinion. In an ideal world, authors should be more concerned about proper semantic identification of chunks, and less about predetermining the behavior of the chunks. In the emerging adaptive content universe, window behaviors may depend on which responsive theme or user preference is in effect at request time; the outgoing semantic "thing" is modified only as necessary by transformation (DOM or regex, server or browser). In this way, window behaviors CAN be up to the user rather than an author in a CMS. – Don Day Apr 25 '15 at 19:43
13

it's by the easiest way to open a new window for something like a PDF

It's also the easiest way to annoy non-Windows users. PDF open just fine in browsers on other platforms. Opening a new window also messes up the navigation history and complicates matter on smaller platforms like smartphones.

Do NOT open new windows for things like PDF just because older versions of Windows were broken.

Andrew T.
  • 4,701
  • 8
  • 43
  • 62
Homer
  • 147
  • 1
  • 3
  • 13
    Know what helps when you tell people not to use something? Showing them what *to* use, the HTML5 download attribute: **etc**. – John Aug 31 '15 at 18:07
8

Most web developers use target="_blank" only to open links in new tab. If you use target="_blank" only to open links in a new tab, then it is vulnerable to an attacker. When you open a link in a new tab ( target="_blank" ), the page that opens in a new tab can access the initial tab and change its location using the window.opener property.

Javascript code:

window.opener.location.replace(malicious URL)

Prevention:

rel="nofollow noopener noreferrer"

More about the attribute values.

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
  • 1
    I'm not sure nofollow is related to security – it's for search engine bots. – Darryl Hein Jun 26 '17 at 03:18
  • @DarrylHein noreferrer too – Smart Manoj Oct 17 '20 at 13:25
  • You cannot, as a developer, force links to open in a new tab. You always and only tell the browser to open a new window. That's a big difference, even though browsers (with or without addons) allow you to define how to handle those cases, e. g. open a new tab. – fabian-mcfly Apr 01 '22 at 11:14
7

While target is still acceptable in HTML5 it is not preferred. To link to a PDF file use the download attribute instead of the target attribute.

Here is an example:

<a href="files/invoice.pdf" download>Invoice</a>

If the original file name is coded for unique file storage you can specify a user-friendly download name by assigning a value to the download attribute:

<a href="files/j24oHPqJiUR2ftK0oeNH.pdf" download="invoice.pdf">Invoice</a>

Keep in mind that while most modern browsers support this feature some may not. See caniuse.com for more info.

kojow7
  • 10,308
  • 17
  • 80
  • 135
2

It sure is!

http://www.w3.org/TR/2010/WD-html5-20100624/text-level-semantics.html#the-a-element

Brendan
  • 1,853
  • 11
  • 18
0

I think target attribute is deprecated for the <link> element, not <a>, that's probably why you heard it's not supposed to be used anymore.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Erik Bi
  • 11
-1

You can do it in the following way with jquery, this will open it in a new window:

<input type="button" id="idboton" value="google" name="boton" /> 

<script type="text/javascript">
    $('#idboton').click(function(){
        window.open('https://www.google.com.co');
    });

</script>
  • 1
    Why not add a form around the button with target="_blank". Seems like that would be easier and remove the need for JS. – Darryl Hein Feb 23 '18 at 16:30