<?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; JavaScript</title>
	<atom:link href="http://www.thebrightlines.com/category/javascript/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>Misconceptions on URL encoding and replace() in JavaScript</title>
		<link>http://www.thebrightlines.com/2010/04/08/misconceptions-on-url-encoding-and-replace-in-javascript/</link>
		<comments>http://www.thebrightlines.com/2010/04/08/misconceptions-on-url-encoding-and-replace-in-javascript/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 21:26:34 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[encodeURIComponent]]></category>
		<category><![CDATA[escape]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[replace]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=569</guid>
		<description><![CDATA[JavaScript methods that seems to do what they're supposed to do, but not always.]]></description>
			<content:encoded><![CDATA[<h2>Escape</h2>
<p>Take for example the method of URL encoding a piece of string. There&#8217;s a pretty good chance that you run into a page that advises to use the <code>escape()</code> method for that. And although it <em>seems</em> to work, it will only handle ASCII-characters. You should use the newer method <code>encodeURIComponent()</code> for that.</p>
<p>The reason why <code>escape()</code> is still popular is probably because that method has been around longer, seems to do what you <em>think</em> it should do and is easier to remember. But if you use it for URL encoding, the end result will be buggy. Just <a href="http://www.google.nl/search?num=30&amp;hl=en&amp;client=firefox-a&amp;hs=qmJ&amp;rls=org.mozilla%3Aen-US%3Aofficial&amp;q=escape+javascript+url&amp;aq=f&amp;aqi=&amp;aql=&amp;oq=&amp;gs_rfai=">Google for &#8216;escape JavaScript URL&#8217;</a> and you&#8217;ll find lots of misinformation on this subject. <a href="http://xkr.us/articles/javascript/encode-compare/">xkr.us has a a good web page on that subject</a>.</p>
<h2>Replace</h2>
<p>Another one: <code>replace()</code>. That too seems to do what you think it should. Right? Well, not really. Only the first occurrence of a given string is replaced with another. Once again this is a method that pops up when you search for something like that and quickly copy-paste into your code. If you want to replace all strings you&#8217;ll have to use regular expressions. Instead of <code>replace('foo', 'bar')</code> you&#8217;ll have to write <code>replace(/foo/g, 'bar')</code>. Please pay attention to the lowercase &#8216;g&#8217; after the regular expression. That&#8217;s what toggles the <strong>G</strong>lobal replace.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2010/04/08/misconceptions-on-url-encoding-and-replace-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Document your Javascript: interview with Michael Mathews of JSDoc Toolkit</title>
		<link>http://www.thebrightlines.com/2010/03/28/document-your-javascript-interview-with-michael-mathews-of-jsdoc/</link>
		<comments>http://www.thebrightlines.com/2010/03/28/document-your-javascript-interview-with-michael-mathews-of-jsdoc/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 20:19:45 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[jsdoc]]></category>
		<category><![CDATA[MichaelMathews]]></category>
		<category><![CDATA[micmath]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=525</guid>
		<description><![CDATA[Documenting source code is something that doesn't always get the priority it deserves. Especially Javascript which can be implemented rather ad-hoc. But there are enough tools around that can make documentation a lot easier.]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignright" style="width: 200px"><img title="Michael Mathews" src="/article-data/images/micmath.jpg" alt="Michael Mathews" width="190" height="229" /><p class="wp-caption-text">Michael Mathews</p></div>
<p>I recently discovered <a href="http://code.google.com/p/jsdoc-toolkit/">JSDoc Toolkit</a>, a tool that generates documentation of your Javascript code in the form of a website. That was dearly needed because I wanted to give me and other team members a better overview of my growing JavaScript codebase.</p>
<h2>The history of JSDoc</h2>
<p>The JSDoc-toolkit and its predecessor JSDoc have been around for nearly 10 years now and in both cases the bulk of the code is programmed by Michael Mathews, often shortened to &#8220;micmath&#8221;. JSDoc started as a solution for the growing Javascript code at the company he worked for: &#8220;I couldn&#8217;t find any existing tool that did what I wanted, so I just naturally started building my own. I didn&#8217;t want to totally reinvent the wheel of course. I basically tried to port Sun&#8217;s JavaDoc idea over to JavaScript.&#8221; So he wrote a documentation tool in Perl. &#8220;Once it was done and we&#8217;d used it a bit, I decided to release it as open source on SourceForge. I figured if I found it useful then maybe someone else would as well.&#8221;</p>
<p>Although JSDoc is a very useful documentation tool and probably the first free tool as well, it wasn&#8217;t received that well back in 2001 according to Michael Mathews: &#8220;It seemed the general JavaScript community was not yet ready for JSDoc. Back then the majority of folks doing JavaScript were not in the habit of practicing what you would call &#8216;Software Engineering&#8217; best practice! It was, more often than not, a field dominated scripting cowboys. The idea of automatic documentation generation, I think, was seen as something unneeded by most. According to the stats from SourceForge, the JSDoc tool was only downloaded a few hundred times in the first several years &#8211; and frankly that already seemed amazing enough to me.&#8221;</p>
<h2>The renaissance of JavaScript</h2>
<p>But as the web got more mature, so did the developers. Javascript gained respect in the last few years and the emerge of large libraries such as YUI, jQuery and Prototype showed that Javascript can be used in a complex but constructive way. New ways of coding were used to structure Javascript. Michael noticed the trend: &#8220;Since about 2005 JSDoc really started to take off. Since then it&#8217;s been downloaded over 30,000 times. It still seems amazing to me.&#8221; The latest version of JSDoc Toolkit is downloaded almost 5000 times since it was published in September 2009.</p>
<h2>JSDoc in the wild</h2>
<p>The term JSDoc by the way is a bit confusing. It refers both to the syntax and the program. The syntax has been relatively successful as well: &#8220;I know that a few of Google&#8217;s projects use JsDoc comments to extend their functionality. Closure Compiler is a good example of that, though it can be a pain when other projects start redefining the JsDoc syntax for their own purposes in ways that are incompatible with the original project. Of course there is no official standard for JsDoc comments, so I can&#8217;t blame them, but that is something I&#8217;d really like to see: an official JsDoc standard, I mean.&#8221;</p>
<p>&#8220;The <em>idea</em> of JSDoc has been picked up by many large projects over the years. However, my application was originally written in Perl and wasn&#8217;t very flexible, consequently it got rewritten by others a few times for internal use only. I don&#8217;t want to name any names because I personally think that, while perfectly legal to do that, it violates the open source spirit I intended the work to be used in.&#8221;</p>
<div class="wp-caption alignnone" style="width: 450px"><img title="Document your code..." src="/article-data/images/micmath-4.png" alt="Document your code..." width="440" height="266" /><p class="wp-caption-text">Document your code...</p></div>
<div class="wp-caption alignnone" style="width: 450px"><img title="...run the tool" src="/article-data/images/micmath-2.png" alt="...run the tool" width="440" height="221" /><p class="wp-caption-text">...run the tool...</p></div>
<div class="wp-caption alignnone" style="width: 450px"><img title="... and publish the generated documentation website" src="/article-data/images/micmath-3.png" alt="... and publish the generated documentation website" width="440" height="266" /><p class="wp-caption-text">...and publish the generated documentation website.</p></div>
<h2>The Toolkit of JSDoc</h2>
<p>A year after JSDoc was released, Michael resigned as an active contributor to the project. Gabriel Reid took care of JSDoc after that. He maintained and extended it. But around 2006 Michael decided to do a major upgrade for JSDoc. &#8220;Big frameworks were the norm and along with that was both the need for API documentation as well as the complexity of writing documentation for all the new and interesting things people were doing with JavaScript. I was beginning to think that, in order to get JSDoc up to speed with this new world, it would need to be rewritten from scratch. So I started a discussion about that on the users&#8217; list. That eventually led to the current incarnation of the project: JsDoc Toolkit.&#8221;</p>
<p>The rewrite was written in Javascript itself. That was possible with the open source <a href="http://www.mozilla.org/rhino/">Rhino</a> platform that parses the Javascript. That wasn&#8217;t based on any technical issue: &#8220;I just liked the idea of a program parsing itself. As a bonus the users were more likely to be able to modify the source code if it was written in a language they work in.&#8221;</p>
<p>Rhino runs on Java and was  initially developed by Netscape. Rhino would have been a part of a  larger project to port Netscape Navigator to the Java platform, but that  never happened. Rhino is still being used and is maintained by the  Mozilla Foundation.</p>
<h2>How JSDoc works</h2>
<p>JSDoc reads the code and the comments. The comments need to be formatted according to the JSDoc convention. It&#8217;s a notation that&#8217;s used in other tools like Google&#8217;s Closure as well and is similar to the JavaDoc convention.</p>
<p>The comments give hints to JSDoc Toolkit how the JavaScript is structured. Unfortunately not all notations are recognized by the tool. In fact: most of the really exotic notations are not recognized. Michael: &#8220;People don&#8217;t realize how difficult it can be to do static code analysis on a language as dynamic as JavaScript. Well, at least I didn&#8217;t. It is provably impossible in most cases to know what the various states of a JavaScript program will be at runtime, just from looking at the source code. I tell people, if you can get a program to know what you want have programmed, just from the plain source code, consider yourself lucky!&#8221;</p>
<p>&#8220;But for the rest of us JsDoc Toolkit provides a way to document your code in such a way that the tool never has to even see your code. In fact, you can even use JsDoc Toolkit in files with just comments, no code whatsoever! So in that sense there isn&#8217;t any code you can&#8217;t document with JsDoc Toolkit, as long as you don&#8217;t rely on the static code analysis features.&#8221;</p>
<p>The word &#8220;Toolkit&#8221; in the name &#8220;JSDoc Toolkit&#8221;, by the way refers to the template engine JSPlate. Michael: &#8220;I eventually went with the &#8216;Toolkit&#8217; name mostly because I liked the sound of it, but also as a little nod to a favourite templating system of mine, Andy Wardley&#8217;s Perl Template Toolkit.&#8221;</p>
<h2>JsDoc Toolkit Version 3</h2>
<p>Right now Michael is in the middle of programming version 3 of JSDoc of which the first beta should be released somewhere this summer. &#8220;Basically there are a few feature requests from JsDoc Toolkit 2 that seem reasonable enough -things like documenting the same symbol more than once- which are utterly impossible due to the core architecture of the tool itself. All those feature requests are now on the table and I&#8217;ll try to include them in the new version.&#8221;</p>
<p>&#8220;One feature that will definitely be supported is the idea of overloaded functions. A typical example would be a function named &#8216;width()&#8217; that returns the width when it is called with no arguments, but will set the width when called with a numeric argument.&#8221;</p>
<p>&#8220;This is fundamentally not possible to document in JsDoc Toolkit Version 2. And that really is the point of rewriting the tool for Version 3: it will include, in it&#8217;s core, all the lessons learned from the past several years of supporting JsDoc Toolkit. I believe it will provide a much more flexible foundation to build on.&#8221;</p>
<p>In addition, Michael will make more use of Rhino than he already did. Until now Rhino was only used to parse the server-side JavaScript. The parser that walks over the source code looking for doc comments and things like function declarations was written by Michael himself. Rhino has a JavaScript parser built in, but it would took &#8220;some serious Java hacking&#8221; to get it right.</p>
<p>&#8220;Fast forward a few years and some important things have changed: namely, Norris Boyd has made accessing Rhino&#8217;s internal Abstract Syntax Tree much easier. In fact, it is now as easy to walk over your source code as it is to use, say, DOM nodes to walk over HTML. I&#8217;d be crazy not to take advantage of all that free power just sitting there waiting to be used!&#8221;</p>
<p>&#8220;So Version 3 will have none of my own parser left in it. This means I get to shed about 40% of the project&#8217;s code, which was dedicated to just that one task, and can now focus on the more important bits: parsing the doc comments.&#8221;</p>
<p>&#8220;It also means that, in theory, if Rhino can understand it, you&#8217;ll be able to get JsDoc Toolkit to understand it. Of course this is all taking time to get up to speed with, but I&#8217;ve decided to use build my tool on top of the new <a href="http://ringojs.org/wiki/">RingoJS</a> framework and that&#8217;s made my life much easier, and the process much faster.&#8221;</p>
<p>The first beta of version 3 is estimated to be released in early summer.</p>
<h2>Continuation</h2>
<p>One of the weak spots of the JSDoc project is that most of the time there&#8217;s only one active developer working on it. Although the tool is still alive and kicking, it naturally needs some nurturing to prevent it from getting stale. &#8220;To be honest it can feel like a real burden sometimes.&#8221; Michael admits. &#8220;It&#8217;s essentially just me these days, and I mostly can&#8217;t give the project all the time it deserves.&#8221; So developers are welcomed to join in the project: &#8220;I&#8217;m ready to sign them up! The fact is that I think of JSDoc as my baby. Not that I invented anything new or amazing, I most emphatically didn&#8217;t. But I&#8217;d lived with it long enough and I&#8217;d become associated with it, so I wanted to see it do well in life.&#8221;</p>
<p>And so the development continues even though there are now some other tools around like JSDoc. He even used some of them. &#8220;I used to write a lot of Perl and JavaScript at the same time and found NaturalDocs to be a great tool because it required a single format that could work in any language.&#8221;</p>
<h2>Server side JavaScript</h2>
<p>Although JavaScript as a program language is appreciated much more than a few years ago, it is still a quirky language. But that&#8217;s no problem for Michael: &#8220;Yeh, it&#8217;s a weird language. But I come from a background of Perl, so I like weird languages! But actually, JavaScript on the server side is a totally refreshing experience. Rhino, for example, allows you to use all the latest cutting-edge JavaScript syntax and features, and you never have to worry about running in an ancient or buggy web browser. I hope and expect that server side JavaScript will really start catching on in the next few years.&#8221;</p>
<h2>A bit about Michael Mathews</h2>
<p>It was hard to find a bit of information about New Yorker Michael Mathews. Although JSDoc has been around for almost 10 years and is downloaded a lot, he hasn&#8217;t been interviewed before. He has been asked many times for some assistance though. It can create funny situations: &#8220;I was doing some freelance work and the fellow sitting next to me asked if I&#8217;d ever heard anything about this &#8216;JSDoc&#8217; stuff he was trying to use. I said I had heard of it and helped him sort it out. I had to eventually admit that I was the creator of the tool. He seemed a little surprised by that.&#8221;</p>
<p>Mathews started as a zookeeper, which he had done for a few years. But it wasn&#8217;t that much fun for him. &#8220;I basically cleaned up after lots of unusual animals.&#8221; When he wasn&#8217;t working, he played with his computer. &#8220;By the late nineties I was had my own homepage, complete with the large, requisite, &#8216;Under Construction&#8217; message.&#8221; His work behind the computer payed off eventually: &#8220;in the fall of 1998 I was employed as a Web Developer for MTV Online, working in Times Square in New York City. It still seems amazing to me.</p>
<p>He now works since 2008 for the BBC in London where he works on the <a href="http://www.bbc.co.uk/glow/">Glow</a> JavaScript library. And yes, everything of it is documented with the help of JSDoc Toolkit.</p>
<p>Glow is a JavaScript library that just, like Prototype and jQuery, makes programming in JavaScript easier. The difference is that Glow supports whatever standard is currently defined by the BBC Standards and Guidelines for browsers, accessibility and usability. This could mean it has to support older browsers that other major JavaScript libraries have already dropped.</p>
<h2>Links</h2>
<ul>
<li><a href="http://code.google.com/u/micmath/">JSDoc Toolkit</a></li>
<li><a href="http://twitter.com/jsdoctoolkit">JSDoc Toolkit Twitter</a></li>
<li><a href="http://micmath.github.com/">JSDoc Toolkit 3 Blog</a></li>
<li><a href="http://groups.google.com/group/jsdoc-2?pli=1">JsDoc Toolkit Users Group</a></li>
</ul>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2010/05/06/new-template-for-jsdoctoolkit-codeview/' rel='bookmark' title='Permanent Link: Codeview, a new template for JSDoc Toolkit'>Codeview, a new template for JSDoc Toolkit</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2010/03/28/document-your-javascript-interview-with-michael-mathews-of-jsdoc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>0</slash:comments>
		</item>
		<item>
		<title>Too advanced CSS selectors</title>
		<link>http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/</link>
		<comments>http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 22:27:05 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[selector]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=273</guid>
		<description><![CDATA[CSS offers lots of different selectors, but sometimes it's better not to use them.]]></description>
			<content:encoded><![CDATA[<p>A few years ago the only reasonable way to select an element through CSS was by using classes and/or tag names. If you wanted to target a specific element, you had to select it by using its classname. Selecting elements with tag names wasn&#8217;t always practicle. For example: if you use the selector <code>div.container div</code>, you style all <code>div</code> elements inside the <code>div.container</code>.</p>
<p>Most of the time that&#8217;s not what you want. Using tag names as selectors can also make design changes troublesome if you have to add more <code>div</code>s and need to give it a different styling. But since IE7 we can use new selectors that help us to build much more delicate CSS selectors. Those new selectors are:</p>
<ul>
<li>Child selector: <code>.parent &gt; .child</code></li>
<li>First child selector: <code>.parent .child:first-child</code></li>
<li>Adjacent selector: <code>.sibling + .sibling</code></li>
<li>First child selector: <code>div:first-child</code></li>
<li>Attribute selectors: <code>div[id]</code></li>
</ul>
<p>Now you can style the following HTML with just a single class:</p>
<h3>HTML:</h3>
<pre name="code" class="xhtml:nogutter">
<div class='container'>
	<span>This is some title</span>
	<a href="#">Lorem ipsum</a>
	<a href="#">Lorem ipsum</a>
	<a href="#">Lorem ipsum</a>
	<span>Some body text with lots of <span>lorem <span>ipsums</span></span> in it</span>
	<span>Some body text with lots of <span>lorem <span>ipsums</span></span> in it</span>
	<a href="#">Lorem ipsum</a>
</div>
</pre>
<h3>CSS:</h3>
<pre name="code" class="css:nogutter">
.container span {
	font-family: sans-serif;
}
.container > span:first-child {
	font-size: 1.5em;
	font-weight: bold;
	font-family: serif;
}
.container > span > span {
	font-style: italic;
}
.container > span > span > span {
	font-weight: bold;
}
.container > span + span {
	display: block;
	margin-top: 0.5em;
}
.container a:nth-child(2) {
	font-weight: bold;
}
.container a {
	display: block;
	color: #f0f;
}
.container a:nth-child(4) ~ a {
	font-size: 0.9em;
}
</pre>
<p>The advantage is that you can, as you can see, style whole blocks of HTML code with just one class. In theory you could even use no classes at all. Without &#8216;class&#8217;-attributes on every element, the HTML code looks just a little bit more cleaner and readable. You can also use those and other selectors in javascript libraries like jQuery to select particular HTML elements. Since most libraries are cross-browser you don&#8217;t have to worry if a selector is or isn&#8217;t supported by a browser. Consider this piece of code in jQuery:</p>
<pre name="code" class="javascript:nogutter">
jQuery(".container a:nth-child(4) ~ a").click( function() {
	alert('hello world');
})
</pre>
<h2>That&#8217;s all nice, but should I use it?</h2>
<p>No. I use them some times, but I try to stay away from it if possible. The problem with these selectors is that they rely so much on the structure of the HTML that it becomes harder to change the HTML structure. If you take another look at the HTML and CSS that I showed as an example in the beginning of the article, it&#8217;s clear that you can&#8217;t add a few links or spans without having to change the CSS. Add some advanced jQuery selectors in the mix and you may have locked the HTML structure forever since no-one dares to change it.</p>
<h2>Should I discard those new selectors then?</h2>
<p>Not necessarily. Sometimes you just have no choice if you for some reason are not able to change the HTML. My personal guideline is that styling has to be predictable. So when you edit some piece of HTML code, the styling should behave as expected. That means that you don&#8217;t style your HTML in a way that the text-color of the 4th <code>span</code> is red.</p>
<p>So what can be styled predictable? If you ask me advanced selectors shine when the structure is relatively fixed. Lists are a good example. Just take a look at these code blocks below. The styling will behave normal, regardless what you do with the HTML.</p>
<h3>HTML:</h3>
<pre name="code" class="xhtml:nogutter">
<ul class="list">
<li>Lorem</li>
<li>Ipsum</li>
<li>Dolor
<ul>
<li>Sit</li>
<li>Amet</li>
</ul>
</ul>
</pre>
<h3>CSS:</h3>
<pre name="code" class="css:nogutter">
ul.list li + li {
	margin-top: 10px; /* adds top margin to all list items except the first */
}
ul.list > li {
	font-weight: bold; /* styles only the first level list items differently */
}
</pre>
<h3>Javascript:</h3>
<pre name="code" class="javascript:nogutter">
jQuery("ul.list li:nth-child(even)").addClass("zebra"); // adds zebra striping to a list
</pre>
<h2>HTML elements you can easily apply advanced selectors</h2>
<p>Besides <code>ul</code>, you can style <code>ul</code>s, <code>dl</code>s, <code>table</code>s. That doesn&#8217;t mean that other elements should only be styled with classes. There are always some exceptions to the rule. Just let your common sense be the judge.</p>


<p>Related posts:<ol><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>
<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/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/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/11/27/filtering-ie6-and-ie7-in-quirks-mode/' rel='bookmark' title='Permanent Link: What you can&#8217;t do in IE&#8217;s Quirks Mode'>What you can&#8217;t do in IE&#8217;s Quirks Mode</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Popups dodging popup blockers</title>
		<link>http://www.thebrightlines.com/2009/10/29/popups-dodging-popup-blockers/</link>
		<comments>http://www.thebrightlines.com/2009/10/29/popups-dodging-popup-blockers/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 21:29:41 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[popup]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=66</guid>
		<description><![CDATA[Popup blockers are everywhere, but that doesn't mean we can't do popups anymore.]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="Popups" src="/article-data/images/popups.png" alt="" width="150" height="150" />A few days ago a collegue asked how he could create a link to another page with Javascript that would open in a new window. He reckoned that since the emergence of the popup blockers it&#8217;s impossible to create a popup of any sorts with Javascript. Not true, but understandable. Popup blockers changed the world and protected us ever since from both irritating bannering and designers that didn&#8217;t care about usability. And there are valid solutions like absolute positioned blocks.</p>
<p>Anyway: most popup blockers only block popups that open without any user interaction. If you use an <code>onclick</code> event to open a new window, it will be ignored by convention.</p>
<p>What&#8217;s creepy by the way is that you could open multiple popups in one click like this:</p>
<pre name="code" class="javascript">
<a href="javascript:window.open();window.open();window.open();window.open();void(0)">open popups</a>
</pre>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2009/10/29/popups-dodging-popup-blockers/feed/</wfw:commentRss>
		<slash:comments>0</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>
