views:

1840

answers:

5

so lets say i got these following codes

<div id="link_other"> <ul> <li><a href="http://www.google.com/"&gt;google&lt;/a&gt;&lt;/li&gt; <li><div class="some_class">dsalkfnm sladkfm <a href="http://www.yahoo.com/"&gt;yahoo&lt;/a&gt;&lt;/div&gt; </li> </ul> </div>

so in this case script will add target="_blank" to all links within "link_other" div. how can i do that? thank you

+4  A: 

Using jQuery this is very easy:

 $('#link_other a').each( fcuntion() {
  $(this).attr('target', '_BLANK');
 });
Pim Jager
s/fcuntion/function/
Chris Doggett
Can't you just do: $('#link_other a').attr('target', '_blank'); ?
Outlaw Programmer
shorter is: $('#link_other a').attr('target', '_blank');as artlung posted
Thomas Stock
+10  A: 

Non-jquery:

var linkList = document.getElementById('link_other').getElementsByTagName('a');

for(var i in linkList){
 linkList[i].setAttribute('target', '_blank');
}
Mike Robinson
Instead of getAttributesByTagName, shouldn't it be getElementsByTagName?
Chris Doggett
Good call - fixed
Mike Robinson
+1 for writing vanilla JavaScript.
Zack Mulgrew
Or just linkList[i].target = '_blank';
J-P
+2  A: 

Bear in mind that doing this is considered bad practice in general by web developers and usability experts. Jakob Nielson has this to say about removing control of the users browsing experience:

Avoid spawning multiple browser windows if at all possible — taking the "Back" button away from users can make their experience so painful that it usually far outweighs whatever benefit you're trying to provide. One common theory in favor of spawning the second window is that it keeps users from leaving your site, but ironically it may have just the opposite effect by preventing them from returning when they want to.

I believe this is the rationale for the target attribute being removed by the W3C from the XHTML 1.1 spec.

If you're dead set on taking this approach, Pim Jager's solution is good.

A nicer, more user friendly idea, would be to append a graphic to all of your external links, indicating to the user that following the link will take them externally.

You could do this with jquery:

$('a[href^="http://"]').each(function() {
    $('<img width="10px" height="10px" src="/images/skin/external.png" alt="External Link" />').appendTo(this)

});
Bayard Randel
While I mostly agree with what you're saying, I think target blank and the "rel='external'" trick do have their place, especially when you're linking to a PDF.
Mike Robinson
rel="external" is interesting -- http://www.sitepoint.com/article/standards-compliant-world/3/ -- though it looks like it needs to be used in conjunction with JavaScript to get it to work. It does allow you to avoid using the disallowed xhtml attribute "target" in-line in your html though.Thanks for mentioning it though, Mike. rel="external" is worth following. :-)
artlung
What if a non-external link's href starts with http:// ?
J-P
Jimmy, what would your reasoning be for doing that when you can use a relative path?
Bayard Randel
Reading J.Nielson's name here reminds me of http://bokardo.com/archives/comic-jakob-who/ ;-)
naivists
+10  A: 
/* here are two different ways to do this */
//using jquery:
$(document).ready(function(){
  $('#link_other a').attr('target', '_blank');
});

// not using jquery
window.onload = function(){
  var anchors = document.getElementById('link_other').getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++){
    anchors[i].setAttribute('target', '_blank');
  }
}
// jquery is prettier. :-)

You could also add a title tag to notify the user that you are doing this, to warn them, because as has been pointed out, it's not what users expect:

$('#link_other a').attr('target', '_blank').attr('title','This link will open in a new window.');
artlung
thank you very much. this works like charm
kakkalo
A: 

Inline:

$('#link_other').find('a').attr('target','_blank');
cfree