views:

89

answers:

1

My goal is to make the title of every external link equal to its href attribute.

My question is, how can I make the title variable available to the attr function?

$('a').filter(function() {

 var title= $(this).attr('href');
 return this.hostname && this.hostname !== location.hostname;

 })
 .removeAttr('target')
 .attr('rel', 'external')
 .attr('title', title);

$('a[rel="external"]').click( function() {
    window.open( $(this).attr('href') );
    return false;
});

I think I somehow have it backwards, and the answer is in attr(key, fn)

+3  A: 
$('a').each(function() {
    var href = $(this).attr('href');
    if(this.hostname && this.hostname !== location.hostname) {
        $(this).removeAttr('target')
               .attr('rel', 'external')
               .attr('title', href)
               .click(function() {
                   window.open(href);
                   return false;
               });
    } 
});
Paolo Bergantino
Thanks! Is there a way to combine this with the click function?
meleyal
I changed it. There really is no need to bind a click event to it, though, I'm not sure why you want to do it this way. But I'm sure you have your reasons.
Paolo Bergantino
Our external links should open in their own window. I'd prefer not but rules is rules. Thanks again :)
meleyal