Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to open new browser window?

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.

like image 708
Kon Avatar asked Oct 24 '08 13:10

Kon


People also ask

What is the best way to open a link in a new browser window in HTML?

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.

Is it better to open in new tab or new window?

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.


2 Answers

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:

  • it is very clear semantically: you have a link to an external resource
  • it is standards-compliant
  • it degrades gracefully (you have a very simple link with regular href attribute)
  • it still allows user to middle-click the link and open it in new tab if they wish
like image 88
Damir Zekić Avatar answered Sep 30 '22 08:09

Damir Zekić


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)

like image 35
Luk Avatar answered Sep 30 '22 08:09

Luk