Reputation: 49
I'd like all of my links that are not xxx.domain.com and not "/relative" links to open in an external window. This code shows the problem, which is wrong as the first conditional selector is immediately overwritten by the second. So what I am looking for is multiple conditional selectors and how to combine these two tests, my attempts at which I am having no luck:
try 1)
$("#links a:not([href^='/']), #links a:not([href*='.domain.com/'])").live('click',function(e){
$(this).attr("target", "_blank");
});
try 2)
$("#links a:not([href^='/']).#links a:not([href*='.domain.com/'])
try 3)
$("#links a:not([href^='/']):not([href*='.domain.com/'])"
try 4) (i really had hope for this one)
$("#link a:not([href^='/'])").live('click',function(e){
$(this).attr("target", "_blank");
$("#links a:is([href*='.domain.com/'])").removeAttr("target");
});
<div id="link"><a href="/relative-path.html">relative</a></div>
<div id="link"><a href="http://www.google.com/">offsite</a></div>
<div id="link"><a href="http://many.domain.com/">onsite</a></div>
<div id="link"><a href="http://any.domain.com/">onsite 2</a></div>
<div id="link"><a href="http://domain.com/">onsite 3</a></div>
Thanks for reading!
Upvotes: 2
Views: 622
Reputation: 140236
$(document.links).each( function(){
this.target = ~this.href.indexOf( "domain.com/") ? "" : "_blank";
});
The href property has automatically the expanded url.
Upvotes: 5