<?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>Joe Lanman &#187; javascript</title>
	<atom:link href="http://www.joelanman.com/by_tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.joelanman.com</link>
	<description>Web design and development when not playing computer games</description>
	<lastBuildDate>Sat, 18 Dec 2010 19:10:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Prototyping with jsFiddle.net</title>
		<link>http://www.joelanman.com/2010/07/prototyping-with-jsfiddle-net/</link>
		<comments>http://www.joelanman.com/2010/07/prototyping-with-jsfiddle-net/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 17:49:11 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Front-end]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jsfiddle]]></category>
		<category><![CDATA[web-apps]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=43</guid>
		<description><![CDATA[I&#8217;m a big fan of applications that do a simple thing well; jsFiddle.net is an innovative environment to work on HTML, CSS and JavaScript in the browser. Coded by Piotr Zalewa and designed by Oskar Krawczyk, it&#8217;s a tool I&#8217;ve found really useful for me and my team &#8211; the simple four-pane layout is brilliantly suited to front-end [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of applications that do a simple thing well; <a href="http://jsfiddle.net" class="aga aga_4">jsFiddle.net</a> is an innovative environment to work on HTML, CSS and JavaScript in the browser. Coded by <a href="http://webdev.zalewa.info/" class="aga aga_5">Piotr Zalewa</a> and designed by <a href="http://nouincolor.com/" class="aga aga_6">Oskar Krawczyk</a>, it&#8217;s a tool I&#8217;ve found really useful for me and my team &#8211; the simple four-pane layout is brilliantly suited to front-end development:<br />
<a rel="attachment wp-att-49" href="http://www.joelanman.com/2010/07/prototyping-with-jsfiddle-net/jsfiddle-01-2/" ><img class="alignnone size-medium wp-image-49" style="margin: 20px 0;" title="jsFiddle-01" src="http://www.joelanman.com/wp-content/uploads/2010/07/jsFiddle-011-580x465.png" alt="" width="580" height="465" /></a><span id="more-43"></span></p>
<p><strong>Prototyping</strong></p>
<p>jsFiddle is really fast to work with &#8211; the layout means all your code is available at once, and it comes with &#8216;batteries included&#8217; &#8211; you can select from common JavaScript libraries to have preloaded. All this means it&#8217;s great for prototyping little interaction ideas &#8211; say a menu or dialog box.</p>
<p>Collaboration is easy &#8211; send the url to a colleague for them to comment on your work, or edit it and send you the updated version &#8211; code as conversation. Even versioning is supported- every change adds a number to the URL, eg. http://jsfiddle.net/ATFcS/1 becomes http://jsfiddle.net/ATFcS/2 when saved, so it&#8217;s easy to go back and review older versions &#8211; a brilliant touch.</p>
<p><strong>Teaching</strong></p>
<p><strong><span style="font-weight: normal;">Learning is always easier when you&#8217;re given examples, and being able to then play with the examples is even better. I&#8217;ve found jsFiddle perfect for explaining aspects of web development to people &#8211; I&#8217;ll knock up a simple example with just the features that we&#8217;re discussing, and the recipient can see what I&#8217;m talking about and modify the code to really cement their understanding. There&#8217;s even a lovely <a href="http://piotr.zalewa.info/embedding_jsfiddle" class="aga aga_7">embedded mode</a> &#8211; next time you talk about a JavaScript/CSS/HTML technique  online, why not make it in jsFiddle and embed an interactive example?</span></strong></p>
<p><strong>Debugging</strong></p>
<p><strong><span style="font-weight: normal;">When faced with a bug in a complex design,  it can be hard to track down exactly what&#8217;s causing the issue. I&#8217;ve found it can be very helpful to recreate a simplified set-up in jsFiddle &#8211; if I can replicate the issue it&#8217;s then very fast to manipulate the code to find a solution.</span></strong></p>
<p>So those are a few uses for jsFiddle &#8211; please feel free to leave your own tips and tricks in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2010/07/prototyping-with-jsfiddle-net/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Usable multiple-selection with checkbox lists and jQuery</title>
		<link>http://www.joelanman.com/2009/03/simple-multiple-selection-with-checkbox-lists-and-jquery/</link>
		<comments>http://www.joelanman.com/2009/03/simple-multiple-selection-with-checkbox-lists-and-jquery/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 19:00:51 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Front-end]]></category>
		<category><![CDATA[User Experience]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=11</guid>
		<description><![CDATA[There are two out-of-the-box methods for multiple selection in HTML: checkboxes and the &#60;select&#62; tag. Neither are perfect &#8211; I&#8217;ll be discussing why, and proposing a sort of mashup of the best aspects of both, using jQuery. The vanilla HTML implementations of these two methods are shown below: 1) Select tag: Bad Off the Wall [...]]]></description>
			<content:encoded><![CDATA[<p>There are two out-of-the-box methods for multiple selection in HTML: checkboxes and the &lt;select&gt; tag. Neither are perfect &#8211; I&#8217;ll be discussing why, and proposing a sort of mashup of the best aspects of both, using jQuery.</p>
<p><span id="more-11"></span></p>
<p>The vanilla HTML implementations of these two methods are shown below:</p>
<p><strong>1) Select tag:</strong></p>
<select multiple="multiple" size="3">
<option>Bad</option>
<option>Off the Wall</option>
<option>Thriller</option>
</select>
<p>(Hold ctrl or command to select multiple values)</p>
<p><strong>2) Checkboxes and Label tag:</strong></p>
<div>
<input id="check_a" type="checkbox" /><label for="check_a">Bad</label></div>
<div>
<input id="check_b" type="checkbox" /><label for="check_b">Off the Wall</label></div>
<div>
<input id="check_c" type="checkbox" /><label for="check_c">Thriller</label></div>
<p>Note that the &lt;label&gt; tag allows users to click the label to toggle the checkbox on and off.</p>
<p>In terms of usability,  the checkbox method is more straightforward &#8211; it is obvious how to select multiple values, whereas with the select method we have to tell the user which keys to hold down. In addition to that, if a user has selected multiple items, and then forgets to hold the correct key on the next click, their selection is lost and only the last value clicked is selected. On a list that can be scrolled, it may not even be apparent that the other selections have been lost.</p>
<p>The disadvantages of the checkbox method are:</p>
<ul>
<li>It&#8217;s not obvious that the label can be clicked.</li>
<li>It would be nice if a larger area were clickable, as with the SELECT example</li>
<li>It would be nice if the entire row were highlighted, as with the SELECT example</li>
<li>The values are not scrollable &#8211; adding more will simply show a longer list</li>
</ul>
<p>We&#8217;re going to fix all that with some simple CSS and Javascript (using JQuery).</p>
<p><a href="/static/examples/multiple_selection">Click to see an example page</a></p>
<p><strong>Edit 1 &#8211; 9/4/09:</strong> Simplify code as per Victor&#8217;s comments, and fix refresh issue in Firefox</p>
<p><strong>Edit 2 &#8211; 9/4/09: </strong>Simplified again, using just the label tags, without &lt;a&gt; tags, from the <a href="http://www.reddit.com/r/web_design/comments/8b876/my_blog_post_on_usable_multipleselection_any/" class="aga aga_9">reddit comments</a></p>
<pre><code>
	$(document).ready(function() {

		// make sure labels are drawn in the correct state

		$('label').each(function()
		{

			if ($(this).find(':checkbox').attr('checked'))
				$(this).addClass('selected');

		});

		// toggle label css when checkbox is clicked

		$(':checkbox').click(function(e)
		{

			var checked = $(this).attr('checked');
			$(this).closest('label').toggleClass('selected', checked);

		});

	});
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2009/03/simple-multiple-selection-with-checkbox-lists-and-jquery/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Python-style startswith and endswith string functions in Javascript</title>
		<link>http://www.joelanman.com/2008/07/python-style-startswith-and-endswith-string-functions-in-javascript/</link>
		<comments>http://www.joelanman.com/2008/07/python-style-startswith-and-endswith-string-functions-in-javascript/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 17:06:49 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Front-end]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[strings]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=4</guid>
		<description><![CDATA[The Python language has useful string methods to search for prefixes and suffixes: my_string.startswith('pre') The same thing can be achieved in Javascript using the rather powerful search and replace methods. These methods can either accept strings or regular expressions.  To search for a plain string, use quote marks, for example: my_string.search('tom'); This will return the [...]]]></description>
			<content:encoded><![CDATA[<p>The Python language has useful string methods to search for prefixes and suffixes:</p>
<pre><code>
my_string.startswith('pre')</code></pre>
<p>The same thing can be achieved in Javascript using the rather powerful search and replace methods.</p>
<p><span id="more-4"></span></p>
<p>These methods can either accept strings or regular expressions.  To search for a plain string, use quote marks, for example:</p>
<pre><code>
my_string.search('tom');</code></pre>
<p>This will return the index of the first occurence of &#8216;tom&#8217; in my_string.</p>
<p>To search for a regular expression, use slashes. Regular expressions use special characters to search for particular patterns in strings &#8211; we&#8217;ll just be using the &#8216;starts with&#8217; (^)  and &#8216;ends with&#8217; ($) characters. Therefore, our Python-style startswith and endswith tests looks like this:</p>
<pre><code>
my_string.search(/^prefix/) ;

my_string.search(/suffix$/);</code></pre>
<p><strong>Note from <a href="http://x13n.com/alex" class="aga aga_11">Alex Macmillan</a></strong> (honorary proof reader)</p>
<p>While the Python functions return a boolean value depending on the match, the Javascript versions return the index of the matched string. If they are not found, the return value will be -1, not false.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2008/07/python-style-startswith-and-endswith-string-functions-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using JQuery and Javascript to parse XML to objects</title>
		<link>http://www.joelanman.com/2008/07/using-jquery-and-javascript-to-parse-xml-to-objects/</link>
		<comments>http://www.joelanman.com/2008/07/using-jquery-and-javascript-to-parse-xml-to-objects/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 00:23:34 +0000</pubDate>
		<dc:creator>Joe Lanman</dc:creator>
				<category><![CDATA[Front-end]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.joelanman.com/?p=3</guid>
		<description><![CDATA[Right &#8211; thought I&#8217;d post some actual code as proof of actual development. I wrote the first version of my CMS back-end in PHP, and sent data to a Javascript front-end using JSON. This was fantastically fast and simple &#8211; the string only need be checked for dangerous syntax, and then eval-ed and it becomes [...]]]></description>
			<content:encoded><![CDATA[<p>Right &#8211; thought I&#8217;d post some actual code as proof of actual development.</p>
<p>I wrote the first version of my CMS back-end in PHP, and sent data to a Javascript front-end using JSON. This was fantastically fast and simple &#8211; the string only need be <a href="http://docs.mootools.net/Utilities/JSON" class="aga aga_15">checked for dangerous syntax</a>, and then eval-ed and it becomes Javascript objects ready to use. The <a href="http://www.youtube.com/watch?v=IE9ripDJHng&amp;feature=related" class="aga aga_16">pot-noodle</a> of data formats.</p>
<p><span id="more-3"></span></p>
<p>Pot noodles are all very well but my friend <a href="http://www.x13n.com/" class="aga aga_17">Alex</a> prefers XML. It&#8217;s much more readable, and pretty much every language and its dog can parse it. I wrote a function to turn my Python data into XML which I&#8217;ll post later, but this post is about my Javascript XML parser, as it was harder to get right.</p>
<p>In summary the parser function takes a complex heirarchical XML tree with nodes and attributes, and turns it into equivalent JS objects and properties. The full code as it stands is at the end of the post, I&#8217;ll go through the main steps here:</p>
<ol>
<li>The function accepts the first child of the XML file and a parent object as arguments.</li>
<li>If the node name ends with &#8216;_array&#8217; then it is, unsurprisingly, an array. This is a naming convention, but I couldn&#8217;t see how else to make sure arrays were consistently dealt with.</li>
<li>If the node has no attributes of its own, treat it as an attribute itself. Again, this is a weird convention on my part, so I can use nodes to store large sections of text, instead of putting them in XML attributes, which would be hard to read.</li>
<li>Check whether the object can be pushed into a parent array, if that fails the parent must be an object &#8211; append the the new object to the parent.</li>
<li>Loop the attributes and assign them as properties to the object.</li>
<li>Loop the child nodes and send them to the function with the current array/object as parent.</li>
</ol>
<p><strong>Javascript code:</strong></p>
<pre>
<code>
xmlToObj : function(xml,parent_obj) {

	if (parent_obj == null)
	{

		// base object

		var new_obj = {};
		xml = xml.firstChild;

	} else if (xml.nodeName.search(/_array$/) != -1) {

		// array

		var new_obj = parent_obj[xml.nodeName] = [];

	} else if(xml.attributes.length == 0) {

		// node which is really a string attribute

		try {

			parent_obj[xml.nodeName] = xml.firstChild.nodeValue;

		} catch(error) {

			parent_obj[xml.nodeName] = '';

		}

	} else {

		if (parent_obj instanceof Array) {

			//member of an array

			var new_obj = {};
			parent_obj[$(xml).attr('id')] = new_obj;

		} else {

			// new object

			var new_obj = parent_obj[xml.nodeName] = {};

		}				

	}

	// map xml attributes to object properties

	$(xml.attributes).each(function()
	{
		if (this.nodeValue.toLowerCase() == 'true') {

			new_obj[this.nodeName] = true;

		} else if (this.nodeValue.toLowerCase() == 'false') {

			new_obj[this.nodeName] = false;

		} else {

			new_obj[this.nodeName] = parseInt(this.nodeValue);

		}
	});

	if ($(xml).children().length &gt; 0)
	{

		$(xml).children().each(function()
		{
			page_edit.xmlToObj(this, new_obj);

		});

	}

	return new_obj;

}</code></pre>
<p><strong>XML data</strong></p>
<pre>
<code>
&lt;page active="True" id="1" is_template="False" position="1" template_id="1" visible="True"&gt;
	&lt;title&gt;
		project
	&lt;/title&gt;
	&lt;sections_array&gt;
		&lt;page_section id="1" page_id="1" position="0" section_id="1" visible="True"&gt;
			&lt;section active="True" attribs="None" id="1"&gt;
				&lt;elements_array&gt;
					&lt;section_element element_id="1" id="1" position="0" section_id="1" visible="True"&gt;
						&lt;element active="True" attribs="None" id="1"&gt;
							&lt;content&gt;

								test
							&lt;/content&gt;
							&lt;title&gt;
								title
							&lt;/title&gt;
						&lt;/element&gt;
					&lt;/section_element&gt;
					&lt;section_element element_id="2" id="2" position="1" section_id="1" visible="True"&gt;
						&lt;element active="True" attribs="None" content="None" id="2"&gt;
							&lt;title&gt;
								test
							&lt;/title&gt;

						&lt;/element&gt;
					&lt;/section_element&gt;
				&lt;/elements_array&gt;
				&lt;title&gt;
					test_section
				&lt;/title&gt;
			&lt;/section&gt;
		&lt;/page_section&gt;
		&lt;page_section id="2" page_id="1" position="1" section_id="2" visible="True"&gt;
			&lt;section active="True" attribs="None" id="2"&gt;

				&lt;elements_array&gt;
					&lt;section_element element_id="4" id="4" position="0" section_id="2" visible="True"&gt;
						&lt;element active="None" attribs="None" id="4"&gt;
							&lt;content&gt;
								Hello my name is Joe
							&lt;/content&gt;
							&lt;title&gt;
								Fred
							&lt;/title&gt;
						&lt;/element&gt;
					&lt;/section_element&gt;

					&lt;section_element element_id="3" id="3" position="1" section_id="2" visible="True"&gt;
						&lt;element active="None" attribs="None" id="3"&gt;
							&lt;content&gt;
								some content
							&lt;/content&gt;
							&lt;title&gt;
								Feedy doo da
							&lt;/title&gt;
						&lt;/element&gt;
					&lt;/section_element&gt;
				&lt;/elements_array&gt;

				&lt;title&gt;
					arab
				&lt;/title&gt;
			&lt;/section&gt;
		&lt;/page_section&gt;
	&lt;/sections_array&gt;
&lt;/page&gt;
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.joelanman.com/2008/07/using-jquery-and-javascript-to-parse-xml-to-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.455 seconds -->

