<?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>Karmagination &#187; browser detection</title>
	<atom:link href="http://www.karmagination.com/blog/tag/browser-detection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.karmagination.com/blog</link>
	<description>Creation begins with imagination</description>
	<lastBuildDate>Fri, 25 Sep 2009 03:44:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Browser detection is still useful</title>
		<link>http://www.karmagination.com/blog/2009/07/31/browser-detection-is-still-useful/</link>
		<comments>http://www.karmagination.com/blog/2009/07/31/browser-detection-is-still-useful/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 00:21:19 +0000</pubDate>
		<dc:creator>Kean</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[browser detection]]></category>

		<guid isPermaLink="false">http://www.karmagination.com/blog/?p=73</guid>
		<description><![CDATA[Note: Whether you agree or not to this article please retweet and spread the word so that we can promote a healthy discussion here. Here is what I think happened in the last 10 plus years or so in the cross browser scripting world; pure incompetence -> global object detection (i.e. document.all) -> user agent [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F31%2Fbrowser-detection-is-still-useful%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.karmagination.com%2Fblog%2F2009%2F07%2F31%2Fbrowser-detection-is-still-useful%2F&amp;source=karmagination&amp;style=normal&amp;service=bit.ly&amp;service_api=R_5ed1fb4fa93b5df6c239dff2a25003de" height="61" width="50" /><br />
			</a>
		</div>
<p>Note: Whether you agree or not to this article please retweet and spread the word so that we can promote a healthy discussion here.</p>
<p>Here is what I think happened in the last 10 plus years or so in the cross browser scripting world; pure incompetence -> global object detection (i.e. document.all) -> user agent sniffing -> feature detection (i.e. document.querySelectorAll) -> <a href="http://thinkweb2.com/projects/prototype/feature-detection-all-the-way/">feature simulation</a> -> browser detect + feature detection / feature simulation</p>
<p>Here&#8217;s some take on <a href="http://blog.davglass.com/2009/01/browser-sniffing-vs-object-detection/">object detection vs user agent sniffing</a> which I am not going to discussing in this article.</p>
<p>Browser detect, whether using user agent sniffing or global object detection, is notorious for being unreliable because are highly unpredictable or can be hijacked.</p>
<p>Feature detect lets you know whether the browser has the method but does not tell you if it works correctly.</p>
<p>Feature simulation, will not only detect if the browser supports a certain method but also tells you if it works correctly or not.</p>
<p>It is clear that, feature simulation is the only way to go in future. End of story. Not!</p>
<p><strong>So, where does browser detect come into play?</strong></p>
<p>In modern JavaScript, browser detect has lost its place in cross-browser scripting and there&#8217;s no doubt about that. BUT, browsers are built differently, from different people with different personalities and philosophies and with different tools. Although browsers try their best to adhere to all the standards, the standards does not specify how implement them i.e. which algorithm to use, how to allocate/deallocate etc.</p>
<p>There exists different codes that will achieve the same results. Let&#8217;s look at two pieces of code that delete all descendant nodes from an element.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">&gt;&lt;</span>strong<span style="color: #339933;">&gt;</span>A<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;&lt;</span>strong<span style="color: #339933;">&gt;</span>A<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;&lt;</span>strong<span style="color: #339933;">&gt;</span>A<span style="color: #339933;">&lt;/</span>strong<span style="color: #339933;">&gt;&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// method one</span>
<span style="color: #003366; font-weight: bold;">var</span> node <span style="color: #339933;">=</span> document.<span style="color: #660066;">geElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
node.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// method two</span>
<span style="color: #003366; font-weight: bold;">var</span> node <span style="color: #339933;">=</span> document.<span style="color: #660066;">geElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> fragment <span style="color: #339933;">=</span> document.<span style="color: #660066;">createDocumentFragment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> newEl <span style="color: #339933;">=</span> node.<span style="color: #660066;">cloneNode</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
fragment.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newEl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
node.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">replaceChild</span><span style="color: #009900;">&#40;</span>newEl<span style="color: #339933;">,</span> node<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The benchmark results are <a href="http://blog.stevenlevithan.com/archives/faster-than-innerhtml">here</a>. Both of the code snippets are cross-browser compatible. The Gecko engine runs faster using method two, other browser runs faster using method one. Here&#8217;s how I would use browser detect to do performance optimization.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> node <span style="color: #339933;">=</span> document.<span style="color: #660066;">geElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> fragment<span style="color: #339933;">,</span> newEl<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isGecko<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  fragment <span style="color: #339933;">=</span> document.<span style="color: #660066;">createDocumentFragment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  newEl <span style="color: #339933;">=</span> node.<span style="color: #660066;">cloneNode</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  fragment.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>newEl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  node.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">replaceChild</span><span style="color: #009900;">&#40;</span>newEl<span style="color: #339933;">,</span> node<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  node.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It does not matter whether isGecko returns a false positive or false negative, the code just works. Below is a generalization on how to use browser detect to optimize your code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isSomeBrowser <span style="color: #339933;">&amp;&amp;</span> isSomeBrowserFeatureSupported<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isSomeCrossBrowserFunctionSupported<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To illustrate the performance improvements, here&#8217;s a benchmark using <a href="http://www.karmagination.com/benchmark">Karmagination vs other JS libraries</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.karmagination.com/blog/2009/07/31/browser-detection-is-still-useful/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
