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.
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.
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"]').
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With