I know that most links should be left up to the end-user to decide how to open, but we can't deny that there are times you almost 'have to' force into a new window (for example to maintain data in a form on the current page).
What I'd like to know is what the consensus is on the 'best' way to open a link in a new browser window.
I know that <a href="url" target="_blank">
is out. I also know that <a href="#" onclick="window.open(url);">
isn't ideal for a variety of reasons. I've also tried to completely replace anchors with something like <span onclick="window.open(url);">
and then style the SPAN to look like a link.
One solution I'm leaning towards is <a href="url" rel="external">
and using JavaScript to set all targets to '_blank' on those anchors marked 'external'.
Are there any other ideas? What's better? I'm looking for the most XHTML-compliant and easiest way to do this.
UPDATE: I say target="_blank" is a no no, because I've read in several places that the target attribute is going to be phased out of XHTML.
You just need an anchor ( <a> ) element with three important attributes: The href attribute set to the URL of the page you want to link to. The target attribute set to _blank , which tells the browser to open the link in a new tab/window, depending on the browser's settings.
The big difference here is that browser tabs are easier for users to manage than browser windows. When a new window opens, it covers the user's earlier window. The user is left confused and wondering how to get back. But when a new tab opens, the user can still see their earlier tab at the top.
I am using the last method you proposed. I add rel="external" or something similar and then use jQuery to iterate through all links and assign them a click handler:
$(document).ready(function() { $('a[rel*=external]').click(function(){ window.open($(this).attr('href')); return false; }); });
I find this the best method because:
href
attribute)Why is target="_blank"
a bad idea?
It's supposed to do exactly what you want.
edit: (see comments) point taken, but I do think that using javascript to do such a task can lead to having some people quite upset (those who middle click to open on a new window by habit, and those who use a NoScript extension)
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