<?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>SwellJS - another big fish in the sea &#187; DOM</title>
	<atom:link href="http://blog.justswell.org/tag/dom/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.justswell.org</link>
	<description>well-minded javascript library</description>
	<lastBuildDate>Fri, 20 Aug 2010 20:48:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Clientside getElementsByClassName cross-browser implementation</title>
		<link>http://blog.justswell.org/clientside-getelementsbyclassname-cross-browser-implementation/</link>
		<comments>http://blog.justswell.org/clientside-getelementsbyclassname-cross-browser-implementation/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 17:27:06 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Swell]]></category>
		<category><![CDATA[DOM]]></category>

		<guid isPermaLink="false">http://justswell.org/blog/?p=6</guid>
		<description><![CDATA[We just came up with a &#8220;getElementsByClassName&#8221; cross-browser implementation quite interesting. Most of the A-Grade browsers doesn&#8217;t support getElementsByClassName natively, and in this case you&#8217;ll have to write from scratch this method—excepted in one case: IE8. Although it&#8217;s kinda annoying for a recent browser (which is still in RC1) to not support this W3C recommandation [...]]]></description>
			<content:encoded><![CDATA[<p>We just came up with a &#8220;getElementsByClassName&#8221; cross-browser implementation quite interesting.</p>
<p>Most of the A-Grade browsers doesn&#8217;t support getElementsByClassName natively, and in this case you&#8217;ll have to write from scratch this method—excepted in one case: IE8.</p>
<p>Although it&#8217;s kinda annoying for a recent browser (which is still in RC1) to not support this W3C recommandation natively, it does at least support querySelectAll&#8230; in standards mode.<br />
So we ran several tests to check the difference between IE8 in quirks/standards mode, which is actually noteworthy: 500% gain when using querySelectorAll, but still about twice slower compared to the native getElementsByClassName implementation in Firefox 3.0.6 ; but well, we didn&#8217;t expect that much.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>className<span style="color: #339933;">,</span> root<span style="color: #339933;">,</span> tagName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    root <span style="color: #339933;">=</span> root <span style="color: #339933;">||</span> document.<span style="color: #660066;">body</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Swell.<span style="color: #660066;">Core</span>.<span style="color: #660066;">isString</span><span style="color: #009900;">&#40;</span>root<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        root <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// for native implementations</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> root.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span>className<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// at least try with querySelector (IE8 standards mode)</span>
    <span style="color: #006600; font-style: italic;">// about 5x quicker than below</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>root.<span style="color: #660066;">querySelectorAll</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        tagName <span style="color: #339933;">=</span> tagName <span style="color: #339933;">||</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> root.<span style="color: #660066;">querySelectorAll</span><span style="color: #009900;">&#40;</span>tagName <span style="color: #339933;">+</span> <span style="color: #3366CC;">'.'</span> <span style="color: #339933;">+</span> className<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// and for others... IE7-, IE8 (quirks mode), Firefox 2-, Safari 3.1-, Opera 9-</span>
    <span style="color: #003366; font-weight: bold;">var</span> tagName <span style="color: #339933;">=</span> tagName <span style="color: #339933;">||</span> <span style="color: #3366CC;">'*'</span><span style="color: #339933;">,</span> _tags <span style="color: #339933;">=</span> root.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span>tagName<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> _nodeList <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> _tag<span style="color: #339933;">;</span> _tag <span style="color: #339933;">=</span> _tags<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span>_tag<span style="color: #339933;">,</span> className<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            _nodeList.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>_tag<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> _nodeList<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.justswell.org/clientside-getelementsbyclassname-cross-browser-implementation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

