Q:

Change link target attribute if its href attribute contains certain phrase

I am trying to check all anchors' tags using .each() and set home URLs' target to _self and other non-home URLs' target to _blank.
So far, I got this:
$('a').each(function() {
    var homeURL = 'google.ca';    
    if ( $(this+'[href*='+homeURL+']')) {
        $(this).attr('target','_self');
    }else{
        $(this).attr('target','_blank');
    }  
});

This is also on jsBin here.
For some reason, the non-home URLs set to target="_self". Can anyone explain why?

A:

Try this instead:
if($(this).is("[href*=" + homeURL + "]")){
    $(this).attr('target','_self');
} else {
    $(this).attr('target','_blank');
}

is() returns true if the selected element matches the selector in the function and false if it does not. So, if the current link's href attribute contains google.ca, it'll change its target attribute to _self. Otherwise, it'll set it to _blank.
And, in fact, for more efficiency, you should cache $(this):
var $this = $(this);
if($this.is("[href*=" + homeURL + "]")){
    $this.attr('target','_self');
} else {
    $this.attr('target','_blank');
}

A:

var homeURL = 'google.ca';
$('a').each(function() {        
    $(this).attr('target', (this.href.match( homeURL )) ? '_self' :'_blank');       
});

