<?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>Two Seven &#187; JavaScript</title>
	<atom:link href="http://twoseven.co.nz/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://twoseven.co.nz</link>
	<description>...is Dan Willis blogging about web design and development, usability, standards and pretty much anything!</description>
	<lastBuildDate>Wed, 08 Feb 2012 03:27:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-20339</generator>
		<item>
		<title>JavaScript Decision Tree tool</title>
		<link>http://twoseven.co.nz/2011/02/18/javascript-decision-tree-tool/</link>
		<comments>http://twoseven.co.nz/2011/02/18/javascript-decision-tree-tool/#comments</comments>
		<pubDate>Fri, 18 Feb 2011 03:38:07 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://twoseven.co.nz/?p=199</guid>
		<description><![CDATA[My goal has been to make a JavaScript Decision Tree tool as a new content module for our next site redesign. It needs to work such that the same piece of JavaScript can be utilised without modification for any decision &#8230; <a href="http://twoseven.co.nz/2011/02/18/javascript-decision-tree-tool/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My goal has been to make a JavaScript Decision Tree tool as a new content module for our next site redesign.</p>
<p>It needs to work such that the same piece of JavaScript can be utilised without modification for any decision tree tool we build on the site, with a very simply-defined markup structure, </p>
<p>So I&#8217;ve been self-administering repeated doses of headdesk/facepalm over the last couple of days, but I came back today after a Friday lunch (you know what I mean) and found a solution. It&#8217;s not pretty, with at least two nested &#8216;for&#8217; loops, but it works.</p>
<p>I&#8217;ve created a JSFiddle of it, so you can take a look at it, modify it, laugh at it, etc.<br />
<a href="http://jsfiddle.net/danw/h8CFe/">Open it in JSFiddle</a> or see it embedded below:</p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/danw/h8CFe/embedded/"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://twoseven.co.nz/2011/02/18/javascript-decision-tree-tool/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Charts and graphs and stuff</title>
		<link>http://twoseven.co.nz/2007/12/10/charts-and-graphs-and-stuff/</link>
		<comments>http://twoseven.co.nz/2007/12/10/charts-and-graphs-and-stuff/#comments</comments>
		<pubDate>Mon, 10 Dec 2007 07:02:40 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Commentary]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://twoseven.co.nz/2007/12/10/charts-and-graphs-and-stuff/</guid>
		<description><![CDATA[It&#8217;s funny how things come in droves. A good friend of mine has, over the years, written a few applications that require graphing. He has written them as VB applications and when I&#8217;ve suggested he make them as web-apps, one &#8230; <a href="http://twoseven.co.nz/2007/12/10/charts-and-graphs-and-stuff/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s funny how things come in droves.</p>
<p>A <a href="http://watercooling.co.nz/">good friend of mine</a> has, over the years, written a few applications that require graphing. He has written them as <abbr title="Visual Basic">VB</abbr> applications and when I&#8217;ve suggested he make them as web-apps, one of his fairly valid objections is that it would be tricky to create good-looking graphs with dynamic data.</p>
<h3>Google&#8217;s Charting API</h3>
<p>So just this morning, <a href="http://www.devour.co.nz/2007/12/07/google-releases-charting-api/" title="devour.co.nz: Google Releases Charting API">the Barnacle </a> drew my attention to <a href="http://google-code-updates.blogspot.com/2007/12/embed-charts-in-webpages-with-one-of.html">Google&#8217;s new Charting <abbr title="Application Programming Interface">API</abbr></a> (<a href="http://code.google.com/apis/chart/">Read the Developers&#8217; Guide</a>). It takes some query string parameters and returns your chart as an image.</p>
<p>Check it out:</p>
<p><img alt="Line chart with unevenly spaced data points and lines in red, green and dashed blue" src="http://chart.apis.google.com/chart?cht=lxy&#038;chs=200x125&#038;chd=t:0,30,60,70,90,95,100|20,30,40,50,60,70,80|10,30,40,45,52|100,90,40,20,10|-1|5,33,50,55,7&#038;chco=3072F3,ff0000,00aaaa&#038;chls=2,4,1&#038;chm=s,FF0000,0,-1,5|s,0000ff,1,-1,5|s,00aa00,2,-1,5"/></p>
<p><img alt="Line chart with a pale blue horizontal band stretching from 25 percent to 75 percent of the way up the y-axis and a thin horizontal line ten percent of the way up the x-axis" src="http://chart.apis.google.com/chart?cht=lc&#038;chd=s:9gounjqGJD&#038;chco=008000&#038;chls=2.0,4.0,1.0&#038;chxt=x,y&#038;chxl=0:|Sep|Oct|Nov|Dec|1:||50|100&#038;chs=200x125&#038;chm=r,E5ECF9,0,0.75,0.25|r,000000,0,0.1,0.11"/> <img alt="Three dimensional pie chart with May, June, July, August, September and October labels for each segment" src="http://chart.apis.google.com/chart?cht=p3&#038;chs=200x100&#038;chd=s:Hellob&#038;chl=May|Jun|Jul|Aug|Sep|Oct"/></p>
<p>Now that&#8217;s pretty cool, isn&#8217;t it?<br />
Although I think the data encoding, while clever, seems a little quirky.</p>
<h3>Flot for jQuery</h3>
<p>And so having spotted that this morning, I&#8217;m stumbling around the net this evening and discover <a href="http://code.google.com/p/flot/">Flot</a>, a charting plug-in for <a href="http://jquery.com/" title="jQuery: The Write Less, Do More, JavaScript Library">jQuery</a>. This is teh awesome; just <a href="http://people.iola.dk/olau/flot/examples/">check out the examples</a>, especially the <a href="http://people.iola.dk/olau/flot/examples/zooming.html">zoom</a>!</p>
<p>So, while you&#8217;re waiting for other browsers to support SVG, here&#8217;s a veritable choice of quality charting goodness to satisfy the most particular of needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://twoseven.co.nz/2007/12/10/charts-and-graphs-and-stuff/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>IE: How to properly destroy your &#8216;select&#8217; elements with innerHTML!</title>
		<link>http://twoseven.co.nz/2007/10/02/ie-how-to-destroy-your-select-elements-with-innerhtml/</link>
		<comments>http://twoseven.co.nz/2007/10/02/ie-how-to-destroy-your-select-elements-with-innerhtml/#comments</comments>
		<pubDate>Tue, 02 Oct 2007 07:14:53 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://twoseven.co.nz/2007/10/02/ie-how-to-destroy-your-select-elements-with-innerhtml/</guid>
		<description><![CDATA[I know that some people, myself included, say that you&#8217;re not supposed to use innerHTML. And yet for expediency&#8217;s sake, I find myself using innerHTML. Problem I&#8217;ve got an AJAX request to a JSP page which in turn performs an &#8230; <a href="http://twoseven.co.nz/2007/10/02/ie-how-to-destroy-your-select-elements-with-innerhtml/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I know that some people, myself included, say that you&#8217;re not supposed to use innerHTML.<br />
And yet for expediency&#8217;s sake, I find myself using innerHTML.</p>
<h3>Problem</h3>
<p>I&#8217;ve got an <acronym title="Asynchronous Javascript And XML">AJAX</acronym> request to a <acronym title="Java Server Pages">JSP</acronym> page which in turn performs an <acronym title="eXtensible Stylesheet Language">XSL</acronym> transform on an <acronym title="eXtensible Markup Language">XML</acronym> file and returns a bunch of <acronym title="HyperText Markup Language">HTML</acronym> <code>&lt;option&gt;</code> elements that JavaScript inserts into an existing <code>&lt;select&gt;</code> element using <code>innerHTML</code>.</p>
<p>Firefox does it fine, as you&#8217;d expect, but <acronym title="Internet Exploder">IE</acronym> wasn&#8217;t playing nice. The resultant markup was broken, with a missing first opening <code>&lt;option&gt;</code> tag, and some random empty <code>&lt;option&gt;</code> element on the end. </p>
<p>In short, nothing showed up.</p>
<h3>Solution</h3>
<p>So I turned to <a href="http://www.google.co.nz/search?q=IE+innerHTML+select">google</a> and found: <a href="http://support.microsoft.com/kb/276228">BUG: Internet Explorer Fails to Set the innerHTML Property of the Select Object</a>.</p>
<p>Thanks Microsoft, you just make it so easy. Not even.</p>
<p>I think the best solution here is to do it properly, and change my JSP to return a <acronym title="JavaScript Object Notation">JSON</acronym> object or something, and use <code>createElement</code> instead. I know innerHTML is faster, but it&#8217;s kinda not proper.</p>
]]></content:encoded>
			<wfw:commentRss>http://twoseven.co.nz/2007/10/02/ie-how-to-destroy-your-select-elements-with-innerhtml/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How I discovered &#8216;cloneNode&#8217;</title>
		<link>http://twoseven.co.nz/2007/09/26/how-i-discovered-clonenode/</link>
		<comments>http://twoseven.co.nz/2007/09/26/how-i-discovered-clonenode/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 10:01:31 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://twoseven.co.nz/2007/09/26/how-i-discovered-clonenode/</guid>
		<description><![CDATA[So I was trying to copy all the &#60;option&#62; elements from one &#60;select&#62; element to another. I had a drop-down list of countries, and I wanted users to be able to select more than one country, so had a &#8216;Add &#8230; <a href="http://twoseven.co.nz/2007/09/26/how-i-discovered-clonenode/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So I was trying to copy all the <code>&lt;option&gt;</code> elements from one <code>&lt;select&gt;</code> element to another. </p>
<p>I had a drop-down list of countries, and I wanted users to be able to select more than one country, so had a &#8216;Add another country&#8217; button that created a new drop-down; copying into it the <code>&lt;option&gt;</code> elements from the first drop-down.<span id="more-57"></span></p>
<p>I did the old </p>
<pre><code>var someVar = firstSelect.getElementsByTagName('option');</code></pre>
<p>and then ran a <code>for</code> loop that wrote (using <code>appendChild</code>) all the objects in <code>someVar</code> to a new <code>&lt;select&gt;</code> element. I thought this should be sweet.</p>
<p>But no. It seemed that random <code>option</code> elements were disappearing from the initial <code>&lt;select&gt;</code> element, such that after you&#8217;d added a few extra country drop-downs, there were no <code>&lt;option&gt;</code> elements left in the first drop-down to copy from!</p>
<p>As it turns out, <code>someVar</code> actually contains the original <code>option</code> elements themselves, and so I was moving them, instead of copying them.</p>
<h3>cloneNode</h3>
<p>So, after a bit of googling, I discovered <code>cloneNode</code>. </p>
<p>Using </p>
<pre><code>var justATempVar = someVar[i].cloneNode(true);</code></pre>
<p>in my loop; then writing <code>justATempVar</code> to the new <code>&lt;select&gt;</code> element worked a treat; the original <code>&lt;select&gt;</code> <code>&lt;option&gt;</code>s were left unmolested, and I got my new nodes. </p>
<p>In hindsight, I should have just cloned the <code>&lt;select&gt;</code> node and renamed it, which I will probably do when I get in to work tomorrow. I&#8217;m not sure which method is more resource-hungry, but I&#8217;m guessing it&#8217;s the loop.</p>
<p>I&#8217;m not sure whether I&#8217;m supposed to be using <code>cloneNode</code> or not, but it seems to work in the versions of IE and Firefox that I&#8217;m developing in. Proper testing is yet to see if it works in all our supported browsers.</p>
<p><acronym title="For Your Information">FYI</acronym>: The boolean parameter that <code>cloneNode</code> accepts tells the method whether or not to clone child nodes also. This is <a href="http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/cloneNodeSyntaxParametersandNote.htm" title="'cloneNode()' Syntax, Parameters and Note : cloneNode : Javascript Methods : JavaScript Reference examples (example source code) Organized by topic">where I found out about <code>cloneNode</code></a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://twoseven.co.nz/2007/09/26/how-i-discovered-clonenode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JS vs. IE; round two.</title>
		<link>http://twoseven.co.nz/2007/07/20/js-vs-ie-round-two/</link>
		<comments>http://twoseven.co.nz/2007/07/20/js-vs-ie-round-two/#comments</comments>
		<pubDate>Fri, 20 Jul 2007 00:05:35 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://twoseven.co.nz/2007/07/20/js-vs-ie-round-two/</guid>
		<description><![CDATA[So I learned (the hard way) that IE doesn&#8217;t like it when you create JS variables with the same name as the id of an element in the code. Thanks, Stuart.]]></description>
			<content:encoded><![CDATA[<p>So I learned (the hard way) that <acronym title="Microsoft Internet Explorer">IE</acronym> doesn&#8217;t like it when you create <acronym title="JavaScript">JS</acronym> variables with the same name as the id of an element in the code.</p>
<p>Thanks, <a href="http://stubblog.wordpress.com/2007/02/06/documentgetelementbyid-vs-ie/">Stuart</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://twoseven.co.nz/2007/07/20/js-vs-ie-round-two/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Irksome: Internet Explorer vs. JavaScript</title>
		<link>http://twoseven.co.nz/2007/07/16/irksome-internet-explorer-vs-javascript/</link>
		<comments>http://twoseven.co.nz/2007/07/16/irksome-internet-explorer-vs-javascript/#comments</comments>
		<pubDate>Mon, 16 Jul 2007 06:10:36 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Web Standards]]></category>

		<guid isPermaLink="false">http://twoseven.co.nz/2007/07/16/irksome-internet-explorer-vs-javascript/</guid>
		<description><![CDATA[So I&#8217;m coding a form and I want the user to be able to add more rows to it, if they wish. So when the user activates a control, I use JS to create some &#8216;input&#8217; elements (including some radio &#8230; <a href="http://twoseven.co.nz/2007/07/16/irksome-internet-explorer-vs-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m coding a form and I want the user to be able to add more rows to it, if they wish.</p>
<p>So when the user activates a control, I use <acronym title="JavaScript">JS</acronym> to create some &#8216;input&#8217; elements (including some radio buttons), add a few attributes to them, and then insert them in a table cell.<br />It&#8217;s not exactly rocket science, even for a JS <abbr title="noob, short for 'newbie', you noob.">|\|00|3</abbr> such as myself.</p>
<p>Let me expand on this.</p>
<p><span id="more-30"></span></p>
<p>I build in <a href="http://getfirefox.com" title="Get Firefox, if you haven't already">Firefox</a> and test in <acronym title="Internet Explorer (sucks)">IE</acronym>. So I use the following code:</p>
<pre>
// NB: bits and pieces have been left out for the sake of this example.

// create the cell
var newCell = newRow.insertCell(2);

// create the elements
var newInput1 = document.createElement('input' );		
var newInput2 = document.createElement('input' );
var newLabel1 = document.createElement('label');
var newLabel2 = document.createElement('label');

// set some attributes on those elements
newInput1.setAttribute('type', 'radio');
newInput1.setAttribute('name', 'rdoAddRemove_' + rowIndex);		
newInput1.setAttribute('id', 'rdoAdd_' + rowIndex);
newInput1.setAttribute('value', 'Add');
newInput2.setAttribute('type', 'radio');
newInput2.setAttribute('name', 'rdoAddRemove_' + rowIndex);		
newInput2.setAttribute('id', 'rdoRemove_' + rowIndex);
newInput2.setAttribute('value', 'Remove');
newLabel1.setAttribute('for', 'rdoAdd_' + rowIndex);
newLabel2.setAttribute('for', 'rdoRemove_' + rowIndex);

// append the elements to the cell
newCell.appendChild(newInput1);
newCell.appendChild(newLabel1);
newCell.appendChild(newInput2);
newCell.appendChild(newLabel2);
</pre>
<p>So you can see what I&#8217;m trying to do here, and so could Firefox, because it built the code correctly.</p>
<p>IE, on the other hand, displayed the radio buttons, but wouldn&#8217;t allow the user to select them. After much hair-pulling and fretting, (because I can&#8217;t find a <a href="http://www.getfirebug.com/" title="Firebug - best Firefox debugging tool. Ever. Pure pwnage.">Firebug</a> equivalent for IE), I discovered that IE wasn&#8217;t adding the &#8216;name&#8217; attributes to the &#8216;input&#8217; elements.</p>
<p>Apparently (and I&#8217;ve lost the link now) IE has a &#8216;feature&#8217; that means you can&#8217;t change the name of an element once it has been created.</p>
<p>So I tried this:</p>
<pre>
newInput = document.createElement('&lt;input name="rdoAddRemove_' + rowIndex + '" /&gt;' );
</pre>
<p>Yay! It now works in IE! &#8230;but the good browsers spat the dummy. And I really felt that if I were going to do this then I might as well &#8216;innerHTML&#8217; everything.</p>
<p>So I remembered my good old friend <code>try {...} catch(err) {...}</code> and came up with this:</p>
<pre>
try {
	// IE will eat this, FF Safari and others will choke on it.
	newInput1 = document.createElement('&lt;input name="rdoAddRemove_' + rowIndex + '" /&gt;');
	newInput2 = document.createElement('&lt;input name="rdoAddRemove_' + rowIndex + '" /&gt;');
} catch(err) {
	// Good browsers will eat this instead.
	newInput1 = document.createElement('input');		
	newInput2 = document.createElement('input');
}
</pre>
<p>Now whether this is the best way to do it or not, I don&#8217;t know, but it worked in all the browsers I tested it in so I&#8217;m happy(-ish).</p>
<p>GRRRRR @ IE for wasting my afternoon!!</p>
]]></content:encoded>
			<wfw:commentRss>http://twoseven.co.nz/2007/07/16/irksome-internet-explorer-vs-javascript/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

