2

I'm making a script which, automatically ads target="_blank" to all external links. The problem is that, the script also makes the internal absolute links open in a new tab. You can check the problem on this test link: http://www.fairfood.org/testtest/

$("a").filter(function () { return this.hostname && this.hostname !== location.hostname; }).each(function () { $(this).attr({ target: "_blank", title: "Visit " + this.href + " (click to open in a new window)" }); }); 

Does anybody know how to fix this?

Any help is much appreciated.

4
  • That page does have some JS errors such as TWTR is not defined and FB is not defined. Might want to fix those first. Commented May 4, 2011 at 15:02
  • www.fairfood.org != fairfood.org Commented May 4, 2011 at 15:03
  • I don't believe you need the check the hostname here. also, I believe this object is treated differently in other browsers so simply using the not selector w/ your domain name should work. Commented May 4, 2011 at 15:07
  • Thanks for pointing that out pixelboy, I am gonna check those errors. Commented May 9, 2011 at 11:29

4 Answers 4

10

www.yourhost.com is not the same as yourhost.com, so when your links don't match, this isn't working.

You can just take out the www. if you know that this will always lead to a valid URI.

(Also, your use of .each is almost redundant, as jQuery is already knowledgeable about element sets; however, you need it for this.href. Just something to be aware of.)

$("a").filter(function() { return this.hostname && this.hostname.replace(/^www\./, '') !== location.hostname.replace(/^www\./, ''); }).each(function() { $(this).attr({ target: "_blank", title: "Visit " + this.href + " (click to open in a new window)" }); }); 

Live example.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 good solution. @Zoltan don't forget to mark it as the answer if it works. Yeah, I agree, isn't that each an unnecessary loop?
@pixelbobby: See my smaller text. You need it for this.href.
1

You can use a single jQuery selector to do this:

$('a').not('a[href*="fairfood.org/"]').each(function(){ $(this).attr({target: "_blank", title: "Visit " + $(this).href + " (click to open in a new window)"}); }); 

3 Comments

thx. added the .org. and +1'd your comment because I laughed when I read it.
<a href="http://wow.com/thats-a-very-rare-situation-and-you-have-a-better-chance-of-getting-attacked-by-zombies-which-is-pretty-cool-not-going-to-lie-but-ok-fairfood.org.com.de.us.html" target="_blank">occams razor</a>
Also, this.href won't work because you took the function scope of .each away.
0

fairfood.org and www.fairfood.org are technically different hostnames. Get rid of www before you test if you want both to work in your script.

Comments

0

As commented above, www.fairfood.org is not the same host as fairfood.org. Don't treat a WWW and a non-WWW host as one. Instead, choose whether to use www or not; and then stick with it throughout the site.

(This has the added benefit of avoiding potential duplicate content penalties.)

7 Comments

<a href="http://lolsomewhereelsethatsnotfairfood.org/hahah.html">Go here!!!1</a>
@Tomalak Geret'kal I stand corrected and removed the dodgy solution :)
As far as I can tell, the site at the link you give explains that duplicate content penalties would not be applied in this case.
@Tomalak Geret'kal I confess that I just took the first link I stumbled upon. I may be the case that Google does not penalize (and yes, I know that Google ~= internet search), so probably this wasn't the best resource :) However, mixing www and non-www may cause trouble with cookies / cookie based sessions (Unrelated to DCP).
@jensgram: Yea, it could. I promote writing your website such that it doesn't matter, leaving the hostname config to your webserver and keeping it completely abstracted away from your website code. This is largely a matter of personal choice, though.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.