<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Bright Lines &#187; jQuery</title>
	<atom:link href="http://www.thebrightlines.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thebrightlines.com</link>
	<description>HTML, CSS, Javascript and more</description>
	<lastBuildDate>Thu, 29 Jul 2010 22:44:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hack: how to enable :first-child in IE6</title>
		<link>http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/</link>
		<comments>http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 00:43:14 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[first-child]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=433</guid>
		<description><![CDATA[How to implement the CSS selector :first-child in IE6 with a little help from Javascript.]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE:</strong> The pure Javascript routine is updated. It is now as fast as jQuery.</p>
<p>One of the things I most dearly miss in IE6 is the lack of support for the CSS selector <code>:first-child</code>. Some shortcomings in browsers can be patched with Javascript. And <code>:first-child</code> is one of them for sure. If you use a Javascript library like jQuery or Prototype it&#8217;s just one line of Javascript:</p>
<pre name="code" class="xhtml:nogutter">&lt;!--[if lte IE 6]&gt;
    &lt;script type="text/javascript"&gt;
        jQuery("*:first-child").addClass("firstChild")
    &lt;/script&gt;
&lt;![endif]--&gt;</pre>
<p>If you don&#8217;t like Javascript libraries you can use this pure js-code:</p>
<pre name="code" class="xhtml:nogutter">
var tags = document.getElementsByTagName("*"); var nTags = tags.length;
for (var i=0;i<nTags;i++) {
	if (tags[i].firstChild) {
		var el=tags[i].firstChild; while ((el.nodeType!=1)&#038;&#038;(el.nextSibling)) el=el.nextSibling;
		if (el.nodeType==1) el.className += " firstChild";
	}
}
</pre>
<p>Now every first child has the class <code>firstChild</code> that can be used in CSS selectors. You'll get something like this:</p>
<pre name="code" class="css:nogutter">
ul li:first-child,
ul li.firstChild {
    font-weight: bold;
}
</pre>
<h2>The speed</h2>
<p>Throwing around classes through the whole HTML tree is not without a price. Certainly not if you keep in mind that Javascript in IE6 is much slower than in Firefox or Safari. So I tested what would happen if the Javascript would run in the homepage of amazon.com, a page with almost a 1000 tags. Here are the results:</p>
<ul>
<li><strong>Adding firstChild with jQuery:</strong>
<ul>
<li>Amazon.com homepage (1000 tags): +- <strong>215</strong>milliseconds</li>
<li>Five times the Amazon.com homepage (5000 tags): +- <strong>742</strong> milliseconds</li>
</ul>
</li>
<li>Adding firstChild with plain Javascript:
<ul>
<li>Amazon.com homepage (1000 tags): +- <strong>129</strong> milliseconds</li>
<li>Five times the Amazon.com homepage (5000 tags): +- <strong>798</strong> milliseconds</li>
</ul>
</li>
</ul>
<p>As you can see there is not much performance difference. Especially when you keep in mind that every time I ran my test page I got different results. With a page of 5000 tags it could run as fast as 481 milliseconds or as slow as 931.</p>
<h2>Code crafting</h2>
<p>The first test I did with the pure Javascript code gave different results because of a inefficient routine. It ran about 1/3 slower in the 1000-tag page and 4 times slower in a 5000-tag page. After consulting a colleague I was able to find a more efficient routine that could match with jQuery.</p>
<h2>Conclusion</h2>
<p>I think a lag of about 200 milliseconds during a page load with a 1000 tags can be acceptable. Especially since a web page of 1000 tags is already a large website to my standards. Most websites I develop have about 300 tags a page at best. If you want to use this hack, I guess it's best to test the script in the largest pages of the website. The gain isn't that spectacular: just support for :first-child. But on the other side it could really make your life as a web developer just a bit easier.</p>
<p>You might be tempted to patch other shortcomings of IE6 with Javascript. Don't start coding, but consider the <a href="http://code.google.com/p/ie7-js/">IE7.js-script</a>. But I must say up front that I don't have good experiences with it. The script works good in small pages with small CSS files, but you'll experience serious performance issues with medium or large websites.</p>
<h2>Putting it in perspective</h2>
<p>Chrome 4, by the way needed just 37 milliseconds to run the Javascript in the 5000-tag page. That's 20 times faster then IE6.</p>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/' rel='bookmark' title='Permanent Link: Alternative for :nth-of-type() and :nth-child()'>Alternative for :nth-of-type() and :nth-child()</a></li>
<li><a href='http://www.thebrightlines.com/2009/10/28/for-the-sloppy-clickers/' rel='bookmark' title='Permanent Link: For the sloppy clickers'>For the sloppy clickers</a></li>
<li><a href='http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/' rel='bookmark' title='Permanent Link: Too advanced CSS selectors'>Too advanced CSS selectors</a></li>
<li><a href='http://www.thebrightlines.com/2009/11/08/how-to-overcome-the-limits-of-css-in-internet-explorer-6/' rel='bookmark' title='Permanent Link: How to overcome the limits of CSS in Internet Explorer 6'>How to overcome the limits of CSS in Internet Explorer 6</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery 1.4 performance charts</title>
		<link>http://www.thebrightlines.com/2010/01/15/jquery-1-4-performance-charts/</link>
		<comments>http://www.thebrightlines.com/2010/01/15/jquery-1-4-performance-charts/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 11:04:47 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=373</guid>
		<description><![CDATA[There's a new version of jQuery and once again it has become faster. Here are some performance comparisons between jQuery 1.3.2 and 1.4.]]></description>
			<content:encoded><![CDATA[<p>Yesterday jQuery 1.4 was released. They celebrate it with a special website: <a href="http://jquery14.com">jquery14.com</a>. There are a lot new features, but what I definitely like is the fact that performance improves with every release. Here below are a few performance charts from that website. Since the performance is broken down by browser you can see clearly that IE is lagging behind when it comes to Javascript performance.</p>
<p><img class="alignnone" title="The performance of .css() and .attr()" src="http://farm5.static.flickr.com/4026/4271691147_fd72853fa4.jpg" alt="" width="500" height="375" /></p>
<p><img class="alignnone" title="Performance of .css()" src="http://farm5.static.flickr.com/4055/4271691599_8a2f2e0624.jpg" alt="" width="500" height="375" /></p>
<p><img class="alignnone" title="Performance of DOM insertions" src="http://farm5.static.flickr.com/4029/4271691471_1240afd5af.jpg" alt="" width="500" height="375" /></p>
<p><img class="alignnone" title="Performance of .html()" src="http://farm5.static.flickr.com/4037/4271691747_0cce01a33d.jpg" alt="" width="500" height="375" /></p>
<p><img class="alignnone" title="Performance of .remove() and .empty()" src="http://farm3.static.flickr.com/2693/4271690883_3224979b9b.jpg" alt="" width="500" height="375" /></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2010/01/15/jquery-1-4-performance-charts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alternative for :nth-of-type() and :nth-child()</title>
		<link>http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/</link>
		<comments>http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 21:17:14 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[nth-child]]></category>
		<category><![CDATA[nth-of-type]]></category>
		<category><![CDATA[zebra]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=334</guid>
		<description><![CDATA[Replace a CSS3 selector that's not supported in IE with a CSS2 selector that has support.]]></description>
			<content:encoded><![CDATA[<p><a class="button demo" href="/article-data/demo/nthChildAlternative.html"><span> </span>View demo</a></p>
<p>One thing I really like about jQuery is the ease by which you can select the <em>nth</em> item of a list of items like this: <code>jQuery("ul &gt; li:eq(3)")</code>. In CSS you would select an <em>nth</em> item like this: <code>ul &gt; li:nth-of-type(3)</code>. The problem is that <code>:</code><code>nth-of-type</code><code>()</code> is not supported at all in Internet Explorer. IE9 <em>will</em> support all CSS3 selectors, including <code>:</code><code>nth-of-type</code><code>()</code>, but by the time IE9 is a mature browser, Webkit and Gecko-based browsers are be able to do everything except your dishes.</p>
<p>So if you want to make an exception for, say, the third menu item, you have to either add a class to that menu item or select that item through Javascript. But if you don&#8217;t care for IE6, there&#8217;s a reasonable alternative:</p>
<p><code>ul &gt; li:</code><code>nth-of-type</code><code>(3)</code> equals <code>ul &gt; li:first-child + li + li</code>.</p>
<p>Although both selectors are different, they do select the same element. But the first is not supported in IE and the second <em>is</em>. From IE7 and up! Sure, it&#8217;s not feasible to select the 100th item in a list, but not all lists are that long. And you can always use Javascript as a fallback.</p>
<p>This alternative selection method is by the way by no means equal to <code>:</code><code>nth-of-type</code><code>()</code>. With <code>:</code><code>nth-of-type</code><code>()</code> you can make a cool <a href="http://www.alistapart.com/articles/zebratables/">zebra striping</a> without the use of Javascript.</p>
<p>And finally, as suggested in the title, there is also a way to handle <code>:nth-child</code><code>(3)</code> in an almost similar fashion:</p>
<p><code>ul &gt; li:</code><code>nth-child</code><code>(3)</code> equals <code>ul &gt; *:first-child + * + *</code>.</p>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/' rel='bookmark' title='Permanent Link: Hack: how to enable :first-child in IE6'>Hack: how to enable :first-child in IE6</a></li>
<li><a href='http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/' rel='bookmark' title='Permanent Link: Too advanced CSS selectors'>Too advanced CSS selectors</a></li>
<li><a href='http://www.thebrightlines.com/2009/11/08/how-to-overcome-the-limits-of-css-in-internet-explorer-6/' rel='bookmark' title='Permanent Link: How to overcome the limits of CSS in Internet Explorer 6'>How to overcome the limits of CSS in Internet Explorer 6</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>For the sloppy clickers</title>
		<link>http://www.thebrightlines.com/2009/10/28/for-the-sloppy-clickers/</link>
		<comments>http://www.thebrightlines.com/2009/10/28/for-the-sloppy-clickers/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 19:21:23 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Links]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=46</guid>
		<description><![CDATA[Making a text link is one of the easiest things to do when building a websites. It's so easy that we tend to forget alternatives that might be better.]]></description>
			<content:encoded><![CDATA[<p>The best example I can think of is a series of blocks that each contain just a head, an intro and a link to an article, like the one you see here underneath.</p>
<div class="wp-caption alignnone" style="width: 310px"><img title="Articles" src="http://www.thebrightlines.com/article-data/images/articles.png" alt="A list of articles" width="300" height="229" /><p class="wp-caption-text">A list of articles</p></div>
<p>The only linkable items here are the &#8220;Read more&#8221; texts at the bottom of the blocks. The source code looks like this:</p>
<pre name="code" class="html">
<ul class="articles">
<li>
<h2>Lorem ipsum dolor sit amet</h2>

		<span>Suspendisse potenti. Pellentesque nibh ...</span>
		<a href="http://www.thebrightlines.com">Read more</a>
	</li>
</ul>
</pre>
<p>Nothing wrong with the code, but why don&#8217;t we replace the <code>li</code> with an <code>a</code>-tag? That way the whole block is clickable. Much easier for the sloppy clicker. Especially when you use the <code>:hover</code> pseudo-class to add an effect like changing background to make the clickable area even more obvious to the user.</p>
<pre name="code" class="html">
<ul class="articles">
   <a href="http://www.thebrightlines.com">
<h2>Lorem ipsum dolor sit amet</h2>

<span>Suspendisse potenti. Pellentesque nibh ...</span>
    <span class="pseudoLink">Read more</span>
  </a></ul>
</pre>
<pre name="code" class="css">ul.articles a {
	float: left;
	width: 100px;
	margin: 0 0 10px 10px;
	background: #ccc;
}
ul.articles a:hover {
	background: #eee;
}</pre>
<p>If you like to have some control over what Google indexes you might not want that the whole text block will be indexed as important text, but just the heading. In that case you might take a different approach. You could put the link in the head of the article block and let Javascript take care of making the whole blocks linkable. Then you&#8217;ll have to use some code like this:</p>
<pre name="code" class="html">
<ul class="articles">
<li>
<h2><a href="http://www.thebrightlines.com">Lorem ipsum dolor sit amet</a></h2>

<span>Suspendisse potenti. Pellentesque nibh ...</span>
    <span class="pseudoLink">Read more</span></li>
</ul>
</pre>
<pre name="code" class="javascript">
jQuery("ul.articles a:odd").each( function() {
	var anchor = this
	jQuery(this).parent("li").click( function() {
		document.location.href = jQuery(anchor).attr("href")
	})
	jQuery(this).parent("li").mouseover( function() {
		jQuery(this).addClass("alternateBackgroundColor")
	})
	jQuery(this).parent("li").mouseout( function() {
		jQuery(this).removeClass("alternateBackgroundColor")
	})
	jQuery(this).parent("li").addClass("link")
})</pre>
<p>This is just an example, but I hope that I could show you what alternatives you have when you add a link to a website.</p>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/' rel='bookmark' title='Permanent Link: Too advanced CSS selectors'>Too advanced CSS selectors</a></li>
<li><a href='http://www.thebrightlines.com/2009/11/08/how-to-overcome-the-limits-of-css-in-internet-explorer-6/' rel='bookmark' title='Permanent Link: How to overcome the limits of CSS in Internet Explorer 6'>How to overcome the limits of CSS in Internet Explorer 6</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2009/10/28/for-the-sloppy-clickers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
