<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: jQuery tutorial: external links</title>
	<atom:link href="http://www.madeincima.eu/blog/jquery-tutorial-external-links/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.madeincima.eu/blog/jquery-tutorial-external-links/</link>
	<description>A brilliant blog about web design and development</description>
	<lastBuildDate>Sat, 10 Jul 2010 14:44:28 +0200</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Paulo</title>
		<link>http://www.madeincima.eu/blog/jquery-tutorial-external-links/comment-page-1/#comment-199</link>
		<dc:creator>Paulo</dc:creator>
		<pubDate>Sun, 06 Jun 2010 11:10:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.madeincima.eu/?p=192#comment-199</guid>
		<description>The jQuery you used to find all external links is a good reference! I started using it for a project I&#039;m doing, but caught some imperfections. For example, take the following EXTERNAL link:

http://www.externaldomain.com/?q=mydomain.com

Your code treats it as an INTERNAL link since your logic searches for &#039;your domain&#039; anywhere in the href value (since you use the &#039;asterisk equals&#039; operator). To be more robust you would have to find links that start with it by using &#039;carrot equals&#039; operator:

// needs to be repeated for &#039;https://&#039;
$(&quot;a[href^=&#039;http://&#039;]:not([href^=&#039;http://&quot;+location.hostname+&quot;&#039;]));

The code above works almost 100% of the time, but the domain for many sites includes the &#039;www.&#039; prefix and some don&#039;t. So if a link in your body was: &quot;http://www.mydomain.com/page.html&quot; but the address bar of your browser read: &quot;http://mydomain.com&quot;, then it would not match my jQuery above. So we need to be even more precise:

// again, needs to be repeated for &#039;https://&#039;
$(&quot;a[href^=&#039;http://&#039;]:not([href^=&#039;http://&quot;+location.hostname+&quot;&#039;]:not([href^=&#039;http://www.&quot;+location.hostname+&quot;&#039;]));

Personally, this is getting a bit too long, so I preferred to use regular expressions:

// for every link
$(&quot;a&quot;).each(function () {
	var href = $(this).attr(&quot;href&quot;);

	// remove the &#039;www.&#039; prefix from the href value (if it exists)
	var domain = location.hostname.replace(/^(www\.)?/, &quot;&quot;);

	// set up a regular expression that sees if a string starts with http[s]://[www.]mydomain.com
	var re = new RegExp(&quot;^https?:\\/\\/(www.)?&quot; + domain);

	// guaranteed to be an external link
	if (!re.test(href)) {
		$(this).attr(&quot;target&quot;, &quot;_blank&quot;).attr(&quot;title&quot;, &quot;Opens a new window&quot;);
	}
});</description>
		<content:encoded><![CDATA[<p>The jQuery you used to find all external links is a good reference! I started using it for a project I&#8217;m doing, but caught some imperfections. For example, take the following EXTERNAL link:</p>
<p><a href="http://www.externaldomain.com/?q=mydomain.com" rel="nofollow">http://www.externaldomain.com/?q=mydomain.com</a></p>
<p>Your code treats it as an INTERNAL link since your logic searches for &#8216;your domain&#8217; anywhere in the href value (since you use the &#8216;asterisk equals&#8217; operator). To be more robust you would have to find links that start with it by using &#8216;carrot equals&#8217; operator:</p>
<p>// needs to be repeated for &#8216;https://&#8217;<br />
$(&#8220;a[href^='http://']:not([href^='http://"+location.hostname+"']));</p>
<p>The code above works almost 100% of the time, but the domain for many sites includes the &#8216;www.&#8217; prefix and some don&#8217;t. So if a link in your body was: &#8220;http://www.mydomain.com/page.html&#8221; but the address bar of your browser read: &#8220;http://mydomain.com&#8221;, then it would not match my jQuery above. So we need to be even more precise:</p>
<p>// again, needs to be repeated for &#8216;https://&#8217;<br />
$(&#8220;a[href^='http://']:not([href^='http://"+location.hostname+"']:not([href^='http://www."+location.hostname+"']));</p>
<p>Personally, this is getting a bit too long, so I preferred to use regular expressions:</p>
<p>// for every link<br />
$(&#8220;a&#8221;).each(function () {<br />
	var href = $(this).attr(&#8220;href&#8221;);</p>
<p>	// remove the &#8216;www.&#8217; prefix from the href value (if it exists)<br />
	var domain = location.hostname.replace(/^(www\.)?/, &#8220;&#8221;);</p>
<p>	// set up a regular expression that sees if a string starts with http[s]://[www.]mydomain.com<br />
	var re = new RegExp(&#8220;^https?:\\/\\/(www.)?&#8221; + domain);</p>
<p>	// guaranteed to be an external link<br />
	if (!re.test(href)) {<br />
		$(this).attr(&#8220;target&#8221;, &#8220;_blank&#8221;).attr(&#8220;title&#8221;, &#8220;Opens a new window&#8221;);<br />
	}<br />
});</p>
]]></content:encoded>
	</item>
</channel>
</rss>
