Is it alright to use target="_blank" in HTML5?

JavascriptHtmlWindow

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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

Solution 5 - Javascript

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][1] for more info.

[1]: http://caniuse.com/#feat=download "Can I Use: Download attribute"

Solution 6 - Javascript

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.

Solution 7 - Javascript

Solution 8 - Javascript

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.

Solution 9 - Javascript

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarryl HeinView Question on Stackoverflow
Solution 1 - JavascriptmikeView Answer on Stackoverflow
Solution 2 - JavascriptGrahamView Answer on Stackoverflow
Solution 3 - JavascriptchharveyView Answer on Stackoverflow
Solution 4 - JavascriptHomerView Answer on Stackoverflow
Solution 5 - Javascriptkojow7View Answer on Stackoverflow
Solution 6 - JavascriptVamshi KrishnaView Answer on Stackoverflow
Solution 7 - JavascriptBrendanView Answer on Stackoverflow
Solution 8 - JavascriptErik BiView Answer on Stackoverflow
Solution 9 - JavascriptJohan Stiven Hernandez OsorioView Answer on Stackoverflow