<?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>Mike&#039;s Blabberings &#187; javascript</title>
	<atom:link href="http://www.mike-griffith.com/blog/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mike-griffith.com/blog</link>
	<description>on software, testing, and the web.</description>
	<lastBuildDate>Tue, 29 Jun 2010 14:37:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Browser history sniffing with Dojo</title>
		<link>http://www.mike-griffith.com/blog/2010/01/browser-history-sniffing-with-dojo/</link>
		<comments>http://www.mike-griffith.com/blog/2010/01/browser-history-sniffing-with-dojo/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 06:40:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tips & tricks]]></category>

		<guid isPermaLink="false">http://www.mike-griffith.com/blog/?p=346</guid>
		<description><![CDATA[Niall Kennedy posted a now-famous article about using some browser trickery to determine what websites a user on your site has visited.  I&#8217;ve taken that concept and created a module that can be used with the Dojo Toolkit javascript framework.
It provides two methods that you can use in your code, isVisited and isAnyVisited.
One important [...]]]></description>
			<content:encoded><![CDATA[<p>Niall Kennedy posted a <a href="http://www.niallkennedy.com/blog/2008/02/browser-history-sniff.html" target="_blank">now-famous article</a> about using some browser trickery to determine what websites a user on your site has visited.  I&#8217;ve taken that concept and created a module that can be used with the <a href="http://www.dojotoolkit.org/" target="_blank">Dojo Toolkit</a> javascript framework.</p>
<p>It provides two methods that you can use in your code, <code>isVisited</code> and <code>isAnyVisited</code>.</p>
<p>One important note about your URL specification is that you must specify an *exact* URL that the user has been to.  So, for example, if someone has visited several twitter profiles, but never actually went to the homepage or signin screen, then it would be difficult/impossible to tell whether or not they were a twitter user without a massive brute-force query.</p>
<p>You can download <a href="/js/dojo1.4/mdg/sniff.js" target="_blank">sniff.js</a>, or <a href="/code/browsersniff.html" target="_blank">view a demo</a>.</p>
<p><strong>Sample usage:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mdg.sniff&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> usedYahoo <span style="color: #339933;">=</span> mdg.<span style="color: #660066;">sniff</span>.<span style="color: #660066;">isVisited</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.yahoo.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> usedGoogleMaps <span style="color: #339933;">=</span> mdg.<span style="color: #660066;">sniff</span>.<span style="color: #660066;">isAnyVisited</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>
      <span style="color: #3366CC;">&quot;http://maps.google.com&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;http://maps.google.com/maps&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> usedFacebook <span style="color: #339933;">=</span> mdg.<span style="color: #660066;">sniff</span>.<span style="color: #660066;">isAnyVisited</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>
      <span style="color: #3366CC;">&quot;http://www.facebook.com&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;http://www.facebook.com/index.php&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #3366CC;">&quot;https://login.facebook.com/login.php&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Source code:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;mdg.sniff&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">// Browser history sniffing, based on infamous blog post:</span>
<span style="color: #006600; font-style: italic;">// &lt;http://www.niallkennedy.com/blog/2008/02/browser-history-sniff.html&gt;</span>
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">// Sample usage:</span>
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">//    dojo.require(&quot;mdg.sniff&quot;);</span>
<span style="color: #006600; font-style: italic;">//    var usedYahoo = mdg.sniff.isVisited(&quot;http://www.yahoo.com&quot;);</span>
<span style="color: #006600; font-style: italic;">//    var usedGoogleMaps = mdg.sniff.isAnyVisited([</span>
<span style="color: #006600; font-style: italic;">//          &quot;http://maps.google.com&quot;,</span>
<span style="color: #006600; font-style: italic;">//          &quot;http://maps.google.com/maps&quot;]);</span>
<span style="color: #006600; font-style: italic;">//    var usedFacebook = mdg.sniff.isAnyVisited([</span>
<span style="color: #006600; font-style: italic;">//          &quot;http://www.facebook.com&quot;,</span>
<span style="color: #006600; font-style: italic;">//          &quot;http://www.facebook.com/index.php&quot;,</span>
<span style="color: #006600; font-style: italic;">//          &quot;https://login.facebook.com/login.php&quot;]);</span>
<span style="color: #006600; font-style: italic;">//</span>
<span style="color: #006600; font-style: italic;">// Works with Dojo 1.3 and 1.4 (*may* work with 1.2 as well)</span>
<span style="color: #006600; font-style: italic;">//</span>
&nbsp;
dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dojox.html.styles&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> _this <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">sniffCache</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    dojox.<span style="color: #660066;">html</span>.<span style="color: #660066;">insertCssRule</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.dojohistorysniff a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;color:#000000;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    dojox.<span style="color: #660066;">html</span>.<span style="color: #660066;">insertCssRule</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.dojohistorysniff a:visited&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;color:#ff0000 !important;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">isAnyVisited</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*Array*/</span>urls<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>urls.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<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>_this.<span style="color: #660066;">isVisited</span><span style="color: #009900;">&#40;</span>urls<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</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> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">isVisited</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*String*/</span>url<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;">typeof</span><span style="color: #009900;">&#40;</span>_this.<span style="color: #660066;">sniffCache</span><span style="color: #009900;">&#91;</span>url<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> _this.<span style="color: #660066;">sniffCache</span><span style="color: #009900;">&#91;</span>url<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">var</span> link <span style="color: #339933;">=</span> _this.<span style="color: #660066;">addLink</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> color <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> dojo.<span style="color: #660066;">Color</span><span style="color: #009900;">&#40;</span>dojo.<span style="color: #660066;">style</span><span style="color: #009900;">&#40;</span>link<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;color&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>color.<span style="color: #660066;">r</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">255</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            _this.<span style="color: #660066;">sniffCache</span><span style="color: #009900;">&#91;</span>url<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        _this.<span style="color: #660066;">sniffCache</span><span style="color: #009900;">&#91;</span>url<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">insertSniffDiv</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> dojo.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>className<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;dojohistorysniff&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> dojo.<span style="color: #660066;">body</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getSniffDiv</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> divs <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.dojohistorysniff&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>divs.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span> divs<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> _this.<span style="color: #660066;">insertSniffDiv</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addLink</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #006600; font-style: italic;">/*String*/</span>url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> _this.<span style="color: #660066;">getSniffDiv</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> dojo.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>href<span style="color: #339933;">:</span> url<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> div<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * mdg.sniff.isVisited
     * Check whether or not a URL has been visited
     * @param url String
     * @return boolean
     */</span>
    mdg.<span style="color: #660066;">sniff</span>.<span style="color: #660066;">isVisited</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">isVisited</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     * mdg.sniff.isAnyVisited
     * Check whether or not *any* of the URLs specified have been visited
     * @param urls Array of Strings
     * @return boolean
     */</span>
    mdg.<span style="color: #660066;">sniff</span>.<span style="color: #660066;">isAnyVisited</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">isAnyVisited</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2010/01/browser-history-sniffing-with-dojo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fiddler scripting is pretty cool</title>
		<link>http://www.mike-griffith.com/blog/2009/02/fiddler-scripting-is-pretty-cool/</link>
		<comments>http://www.mike-griffith.com/blog/2009/02/fiddler-scripting-is-pretty-cool/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 18:47:56 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[testing]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tips & tricks]]></category>

		<guid isPermaLink="false">http://www.mike-griffith.com/blog/?p=216</guid>
		<description><![CDATA[I never really gave Fiddler all the credit it was due.  I guess I should have read the homepage:
Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language.

I had previously only used it as a session inspector &#8212; to review requests and responses as they occurred in the wild. [...]]]></description>
			<content:encoded><![CDATA[<p>I never really gave <a href="http://www.fiddlertool.com/fiddler/">Fiddler</a> all the credit it was due.  I guess I should have read the homepage:</p>
<blockquote><p>Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language.</p></blockquote>
<p><img src="http://www.mike-griffith.com/blog/wp-content/uploads/2009/02/newfiddlermodified2in-194x300.jpg" alt="fiddler_dancing" title="fiddler_dancing" width="129" height="200" class="alignright size-medium wp-image-229" /></p>
<p>I had previously only used it as a session inspector &#8212; to review requests and responses as they occurred in the wild.  As it turns out, you can modify request and response headers very easily.  </p>
<p>For a quick example, let&#8217;s see what it would take to disable connection keep-alive&#8217;s when cache-control is set to no-cache.  Fire up Fiddler, click Rules, then click &#8220;Customize Rules&#8230;&#8221;.  It will pop open CustomRules.js, a JScript file with a Handler class.  Scroll down to the OnBeforeResponse static function.</p>
<p>Just after the m_DisableCaching block that is probably already defined, let&#8217;s add a simple clause:</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>m_DisableCaching<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    oSession.<span style="color: #660066;">oResponse</span>.<span style="color: #660066;">headers</span>.<span style="color: #660066;">Remove</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Expires&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    oSession.<span style="color: #660066;">oResponse</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Cache-Control&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;no-cache&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>oSession.<span style="color: #660066;">oResponse</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Cache-Control&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;no-cache&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Disable keep-alive by setting Connection: close header</span>
    oSession.<span style="color: #660066;">oResponse</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;Connection&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;close&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Fire up IE, visit a page, and look at the Fiddler session logs to see the difference.  That easy.  Have fun playing with headers!</p>
<p><em>On a side-note: thank God for Wordpress auto-save.  Not that I had much in this article, but I &#8220;tabbed&#8221; out of the text editor accidentally, hit backspace (forcing the browser to go back), and cursed at the keyboard.  Luckily, it was sitting 95% complete in my drafts folder!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2009/02/fiddler-scripting-is-pretty-cool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Readying Javascript for the Enterprise with Automated Testing</title>
		<link>http://www.mike-griffith.com/blog/2009/01/readying-javascript-for-the-enterprise-with-automated-testing/</link>
		<comments>http://www.mike-griffith.com/blog/2009/01/readying-javascript-for-the-enterprise-with-automated-testing/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 04:51:46 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.mike-griffith.com/blog/?p=152</guid>
		<description><![CDATA[&#60;flame&#62;
Javascript seems to gets left in the dust when we consider code quality.  It&#8217;s been rigged, scattered, and smeared every which way from Sunday.  Most sites of significant complexity have a severe amount of legacy, rotting code buried deep in a countless number of files.  There is little separation of concerns in [...]]]></description>
			<content:encoded><![CDATA[<p>&lt;flame&gt;<br />
Javascript seems to gets left in the dust when we consider code quality.  It&#8217;s been rigged, scattered, and smeared every which way from Sunday.  Most sites of significant complexity have a severe amount of legacy, rotting code buried deep in a countless number of files.  There is little separation of concerns in places, where event handlers written alongside presentation markup are validating data and controlling flow simultaneously.  At times, the code gets hacked up at the end of a project to deal with browser flaws and edge cases, and quickly mutates from something not-so-bad to an ugly beast.<br />
&lt;/flame&gt;</p>
<p>Lo and behold, there&#8217;s a way out!  There&#8217;s very few valid excuses for not writing cleaner, prettier, more maintainable Javascript. Many of the same <a href="http://www.python.org/dev/peps/pep-0020/">principles preached in Python</a> hold true.</p>
<p>There are some prevalent concepts that can help us write better code.  Understanding and applying <strong>unobtrusive Javascript</strong> can help alleviate everything from cluttered namespaces to browser differences and performance issues  &#8212; I suggest reading <a href="http://icant.co.uk/articles/seven-rules-of-unobtrusive-javascript/">the 7 rules</a>.  Following directly from that, <strong>progressive enhancement</strong> principles are fantastic to allow for graceful degradation and accessibility.  Do with Javascript what we know we should do with CSS.  Here&#8217;s some <a href="http://www.alistapart.com/articles/progressiveenhancementwithjavascript">food for thought about progressive enhancement</a>.  Additionally, writing <strong>loosely coupled</strong> code (i.e. methods not tied directly to the DOM unless necessary) provides a separation of concerns that helps with debugging and testability.  Finally, automating the testing of your code to ensure it&#8217;s doing what you think it should do (prior to sending it to QA for them to check) is powerful in and of itself.</p>
<p>If you&#8217;ve been bitten by the testing bug and find yourself wanting to test your Javascript code, you&#8217;ve come to the right place.  The first question you should ask yourself is, &#8220;what should I be testing?&#8221;.  I will <strong>focus on unit testing</strong> in this article.  You should also be aware that there are powerful tools (e.g. Selenium) for integration and browser testing.  But first things first.</p>
<p>So, what are common things done in Javascript that might need to be unit-tested?  Here are a few examples:</p>
<ul>
<li>Client-side validation.  Let&#8217;s ensure all bases are covered with validators that are written to check email addresses, birthdays, maximum lengths, etc.</li>
<li>Methods that compute window sizes or element positions.  There&#8217;s often lots of math, and edge cases can be killer.</li>
<li>Basic things ingrained in the Dojo framework, such as class inheritance.  You&#8217;ll want to ensure that your class behaves as you expect.</li>
</ul>
<p>Let&#8217;s take the example of a email address validator and see how we might write it and test it.  Here&#8217;s one method, which happens to validate a string expected to be a valid email address, with a length less than the specified maximum.  This would be located at <code>$JS/dojo1.2/src/app/validators.js</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">provide</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'app.validators'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>email<span style="color: #339933;">,</span> maxLength<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>email.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> maxLength<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pattern <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> pattern.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There are several things we might want to test.  For example, we should test that the maximum length functionality works.  Additionally, we should test that it doesn&#8217;t allow emails without a domain to pass through.  And we don&#8217;t want to leave out testing that a valid email can pass through.  In an ideal world, we would test every reasonable branch through the function, and through the regular expression itself.</p>
<p>Here are 3 test cases, presented in a format acceptable for the D.O.H. framework</p>
<p><strong>Case 1: Too long of an email is invalid</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    doh.<span style="color: #660066;">assertFalse</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'123456'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Case 2: Email without a domain is invalid</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    doh.<span style="color: #660066;">assertFalse</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo@.'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Case 3: A valid email should validate as such</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    doh.<span style="color: #660066;">assertTrue</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo@bar.baz'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We can then insert these into a D.O.H.-compatible test package.  Open an editor to <code>$JS/dojo1.2/src/app/tests/test_validation.js</code>, and register the tests from above as shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'app.validators'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
doh.<span style="color: #660066;">register</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;app.tests.test_validation&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'test_validate_email_too_long'</span><span style="color: #339933;">,</span>
        runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            doh.<span style="color: #660066;">assertFalse</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'123456'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><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: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'test_validate_email_nodomain'</span><span style="color: #339933;">,</span>
        runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            doh.<span style="color: #660066;">assertFalse</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo@.'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><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: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'test_validate_email_ok'</span><span style="color: #339933;">,</span>
        runTest<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            doh.<span style="color: #660066;">assertTrue</span><span style="color: #009900;">&#40;</span>app.<span style="color: #660066;">validators</span>.<span style="color: #660066;">isValidEmail</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo@bar.baz'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><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: #339933;">,</span>
<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The last steps are to plug it into a test &#8220;module&#8221; (so it can be discovered by the D.O.H. runner), and run it to ensure things are working as desired.  Point an editor to <code>$JS/dojo1.2/src/app/tests/module.js</code>, and add the following line:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;app.tests.test_validation&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>D.O.H. provides a browser-based test runner for executing tests.  Assuming you have Dojo 1.2 available on your server, modify the following path to suite your installation: <code>http://<em>servername</em>/js/dojo1.2/src/util/doh/runner.html?testModule=app.tests.module</code>.</p>
<p>Hold tight for the next installment, which will include mocking in Javascript and introduce the Rhino runner.  Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2009/01/readying-javascript-for-the-enterprise-with-automated-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Decorate a method with Dojo</title>
		<link>http://www.mike-griffith.com/blog/2008/10/decorator-pattern-in-dojo/</link>
		<comments>http://www.mike-griffith.com/blog/2008/10/decorator-pattern-in-dojo/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 18:54:35 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.mike-griffith.com/blog/?p=112</guid>
		<description><![CDATA[Here&#8217;s a quick example of how to incorporate the Decorator Pattern into your javascript code using the Dojo framework.  Rather than decorating an entire class, this example merely decorates a single function, extending the original function with some extra logic.

// Instantiate widget class
var w = new core.widget.MyWidget&#40;&#41;;
&#160;
// Decorate the original &#34;method1&#34; function
var _old_method1 = [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick example of how to incorporate the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator Pattern</a> into your javascript code using the Dojo framework.  Rather than decorating an entire class, this example merely decorates a single function, extending the original function with some extra logic.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Instantiate widget class</span>
<span style="color: #003366; font-weight: bold;">var</span> w <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> core.<span style="color: #660066;">widget</span>.<span style="color: #660066;">MyWidget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Decorate the original &quot;method1&quot; function</span>
<span style="color: #003366; font-weight: bold;">var</span> _old_method1 <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span>w<span style="color: #339933;">,</span> w.<span style="color: #660066;">method1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
w.<span style="color: #660066;">method1</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> out <span style="color: #339933;">=</span> _old_method1<span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Decorations</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>param <span style="color: #339933;">==</span> <span style="color: #3366CC;">'foo'</span> <span style="color: #339933;">||</span> out <span style="color: #339933;">==</span> <span style="color: #3366CC;">'bar'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'calling method2 as result of method1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">method2</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'baz'</span><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: #339933;">;</span></pre></div></div>

<p>A few key points:</p>
<ul>
<li>dojo.hitch &#8211; used to provide proper context to the base &#8220;method1&#8243;, so that if anything relies on &#8220;this.&#8221; it will be handled as expected</li>
<li>The decorations can be applied either before or after the base method is called.  In the example, I chose to apply them after the base method to enable reaction based on the output.</li>
</ul>
<p>You may notice that this is a much uglier implementation than the coffee and condiments example in the linked Wikipedia article.  The wrap-add-and-return methodology didn&#8217;t work for me due to some class complexities and black magic.  There may be a better way, perhaps even built into the dojo class wizardry, but this fit my use case well enough.</p>
<p>A coworker pointed out that Dojo now has the fancy shmansy dojox.aspect package to deal with this.  And I found a <a href="http://lazutkin.com/blog/2008/may/18/aop-aspect-javascript-dojo/">great article detailing the new aspect wizardry in dojo</a>.  So my example could be revised using AOP, as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Instantiate widget class</span>
<span style="color: #003366; font-weight: bold;">var</span> w <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> core.<span style="color: #660066;">widget</span>.<span style="color: #660066;">MyWidget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Create an aspect to wrap the method</span>
dojo.<span style="color: #660066;">require</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dojox.lang.aspect&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> aop <span style="color: #339933;">=</span> dojox.<span style="color: #660066;">lang</span>.<span style="color: #660066;">aspect</span><span style="color: #339933;">;</span>
aop.<span style="color: #660066;">advise</span><span style="color: #009900;">&#40;</span>w<span style="color: #339933;">,</span> <span style="color: #3366CC;">'method1'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>around<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> out <span style="color: #339933;">=</span> aop.<span style="color: #660066;">proceed</span><span style="color: #009900;">&#40;</span>param<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Decorations</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>param <span style="color: #339933;">==</span> <span style="color: #3366CC;">'foo'</span> <span style="color: #339933;">||</span> out <span style="color: #339933;">==</span> <span style="color: #3366CC;">'bar'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'calling method2 as result of method1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">method2</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'baz'</span><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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2008/10/decorator-pattern-in-dojo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On my mind today</title>
		<link>http://www.mike-griffith.com/blog/2008/09/on-my-mind-today/</link>
		<comments>http://www.mike-griffith.com/blog/2008/09/on-my-mind-today/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:36:26 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[web security]]></category>

		<guid isPermaLink="false">http://blog.rideshootlive.com/?p=68</guid>
		<description><![CDATA[I&#8217;ve got some ideas that I need to find time to work on.  In no particular order:

Port MockMe to the Dojo javascript framework
Utilize browser history sniffing to build a generalized user segmentation framework
Write a Ubiquity plugin for spreeder (to easily speed read a page in Firefox)
Port OWASP&#8217;s AntiSamy project to Python, to provide comprehensive [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got some ideas that I need to find time to work on.  In no particular order:</p>
<ul>
<li>Port <a href="http://johanneslink.net/projects/mockme.html">MockMe</a> to the Dojo javascript framework</li>
<li>Utilize <a href="http://www.niallkennedy.com/blog/2008/02/browser-history-sniff.html">browser history sniffing</a> to build a generalized user segmentation framework</li>
<li>Write a <a href="http://labs.mozilla.com/projects/ubiquity/">Ubiquity</a> plugin for <a href="http://www.spreeder.com/">spreeder</a> (to easily speed read a page in Firefox)</li>
<li>Port <a href="http://code.google.com/p/owaspantisamy/">OWASP&#8217;s AntiSamy project</a> to Python, to provide comprehensive XSS protection</li>
</ul>
<p>None of these are terribly difficult to get started, and with the summer winding down, I might have a few minutes of free time to start working on some.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2008/09/on-my-mind-today/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun javascript goodies</title>
		<link>http://www.mike-griffith.com/blog/2008/09/fun-javascript-goodies/</link>
		<comments>http://www.mike-griffith.com/blog/2008/09/fun-javascript-goodies/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 16:14:18 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.rideshootlive.com/?p=47</guid>
		<description><![CDATA[Just ran across another of many lists of javascript libraries.  I found 3 things that I&#8217;ll probably end up using, 1 of which was especially interesting.
Sniffer browser history is something I&#8217;ve never thought of or heard of until reading Niall Kennedy&#8217;s post, which apparently he talked briefly about a few years earlier.  The [...]]]></description>
			<content:encoded><![CDATA[<p>Just ran across another of many <a href="http://www.smashingmagazine.com/2008/09/11/75-really-useful-javascript-techniques/">lists of javascript libraries</a>.  I found 3 things that I&#8217;ll <a href="http://www.niallkennedy.com/blog/2008/02/browser-history-sniff.html">probably</a> <a href="http://www.nickstakenburg.com/projects/lightview/">end</a> <a href="http://www.outcut.de/MooFlow/MooFlow.html">up using</a>, 1 of which was especially interesting.</p>
<p>Sniffer browser history is something I&#8217;ve never thought of or heard of until reading <a href="http://www.niallkennedy.com/blog/2008/02/browser-history-sniff.html">Niall Kennedy&#8217;s post</a>, which apparently he talked briefly about a few years earlier.  The idea is that one could list a bunch of known URLs as links on your page.  Then after applying unique a:normal and a:visited styles to them, you can read the computed style values on each link through the DOM.  If the color (for example) changes to the a:visited style that you read through javascript, you now know that the user visited that site, and can make other layout changes accordingly.</p>
<p>Even without javascript sniffing per-se, you could apply the same technique.  Think of this, if we want to do something if the user&#8217;s a yahoo user vs a google user, do this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
a#goog, a#yh {
    display: none; /* hide if user hasn't been there */
}
a#goog:visited, a#yh:visited !important {
    display: inline; /* make sure user sees search engine option if they've been there before */
}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.com&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;goog&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;doGoogle()&quot;</span>&gt;</span>search via google<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.yahoo.com&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;yh&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;doYahoo()&quot;</span>&gt;</span>search via yahoo<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>Niall does have an excellent library started for browser sniffing.  One great feature is that it allows you to configure a set of URLs that correspond to an application (for example www.facebook.com, facebook.com, profile.facebook.com, etc).</p>
<p>Some immediate applications include using the mapping service user is most comfortable with for directions to your business, displaying only necessary social bookmarking &#8220;add to&#8221; icons, and searching via user&#8217;s preferred search engine.  </p>
<p>From the article:</p>
<blockquote><p>Easily recognized branding such as &#8220;Add to My Yahoo&#8221; has yielded much higher conversion rates than a simple Atom link with a minimal effect on page load performance. Dynamically checking for active usage of 50 or so aggregators allows me to extend my total test list and promote an obscure tool that might never make the cut for permanent on-screen real estate.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2008/09/fun-javascript-goodies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cancel dojo XHR in progress</title>
		<link>http://www.mike-griffith.com/blog/2008/09/cancel-dojo-xhr-in-progress/</link>
		<comments>http://www.mike-griffith.com/blog/2008/09/cancel-dojo-xhr-in-progress/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 21:46:33 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://bikegriffith.webfactional.com/blog/?p=27</guid>
		<description><![CDATA[Often you run across a use-case where you want users to be able to fire off an XMLHttpRequest while another is ongoing.  Sometimes, the requests are independent and affect independent objects as a result.   But othertimes, the callback functions from the two could be affecting the same event.
For example, imagine you have a list of [...]]]></description>
			<content:encoded><![CDATA[<p>Often you run across a use-case where you want users to be able to fire off an XMLHttpRequest while another is ongoing.  Sometimes, the requests are independent and affect independent objects as a result.   But othertimes, the callback functions from the two could be affecting the same event.</p>
<p>For example, imagine you have a list of cars in your inventory.  When a user clicks on one of the cars, a request is shot back to determine price, color, inventory stock, and description before displaying it.  If a user clicks on the VW Jetta, then quickly changes their mind and clicks on the Mazda 3, what would happen?  We&#8217;d expect the VW Jetta to be forgotten about and the data to be displayed for the Mazda.  But with all the randomness on the internet, if the 2nd request was quick to be returned from the server, while the 1st got delayed, a poorly designed app would display the Mazda, later receive the callback from the Jetta and overwrite the good results with the one the user had already forgotten about.</p>
<p>We could imagine 2 scenarios for how to stop the possibility of timing issues producing inconsistencies.</p>
<ol>
<li>Block all future car clicks until the initial car click is loaded.  Ugh.  Terrible user experience.</li>
<li>When a car is clicked, see if any pending cars are being queried.  If so, cancel them, and query the new one.</li>
</ol>
<p>How do we make this happen in Dojo 1.0 world?  Simple.  Use the cancel() method given back by the deferred xhr object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">doSingleXhr</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</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>._xhr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">this</span>._xhr.<span style="color: #660066;">cancel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">this</span>._xhr <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">xhrGet</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Almost done.  The one hangup you might see is that the cancel() method does trigger the error() callback if you have one defined in your xhrGet.  Luckily, we can inspect the dojoType of the args given to the callback to determine if it&#8217;s the result of a cancel and ignore it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">doSingleXhr</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</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>._xhr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._xhr.<span style="color: #660066;">cancel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">this</span>._xhr <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">xhrGet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        ...
        <span style="color: #660066;">error</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>args<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>args.<span style="color: #660066;">dojoType</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'cancel'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
            handleError<span style="color: #009900;">&#40;</span><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: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.mike-griffith.com/blog/2008/09/cancel-dojo-xhr-in-progress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
