Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standards-compliant equivalent to target="_blank"

There are instances where I have to open links in a new window/tab. Is there a method of doing so that is valid for strict HTML? Using jQuery to do so would be acceptable, but I'd rather not just sneak the target="_blank"s back in w/ jQuery so that validators won't see them.

like image 374
aslum Avatar asked May 11 '11 17:05

aslum


People also ask

Why target _blank is deprecated?

There's A Security Reason For Not Using _BlankThe target=”_blank” link attribute is risky and opens a website to security and performance issues. Google's Web. dev page on the risks of using the _blank link attribute is summarized as such: “The other page may run on the same process as your page.

What can I use instead of a target blank?

Instead of target="_blank" , use rel="external" for external links. Using the rel-external attribute is better semantics than blank target. And if you need the external link to open in a new tab or window, you can add the following slice of jQuery: $('a[rel="external"]').

What is the meaning of target _blank?

target="_blank" is a special keyword that will open links in a new tab every time. target="blank" will open the first-clicked link in a new tab, but any future links that share target="blank" will open in that same newly-opened tab.

When should I use target _blank?

a target=”_blank” Open in New Browser Tab (or Window) The target attribute specifies where the linked document will open when the link is clicked. The default is the current window. If target="_blank" , the linked document will open in a new tab or (on older browsers) a new window.


1 Answers

Since you said jQuery is allowed.

<a href="http://mysite.com" class="newWindow">Open in new window</a>

$('a.newWindow').click(function(e){
   e.preventDefault();
   window.open(this.href);
});

You could also do this via normal JS. This way your HTML won't have onclick peppered all over the place.

EDIT - Updated to use e.preventDefault() as per Ian's suggestion.

like image 76
JohnP Avatar answered Sep 19 '22 16:09

JohnP