<?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>Cowburn &#187; JavaScript</title>
	<atom:link href="http://cowburn.info/category/js/feed/" rel="self" type="application/rss+xml" />
	<link>http://cowburn.info</link>
	<description>Online storage depot for Peter Cowburn</description>
	<lastBuildDate>Thu, 22 Jul 2010 17:51:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Feed widget with YQL &amp; jQuery</title>
		<link>http://cowburn.info/2009/01/20/feed-widget-with-yql-jquery/</link>
		<comments>http://cowburn.info/2009/01/20/feed-widget-with-yql-jquery/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 13:23:38 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[YQL]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Salathe]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=99</guid>
		<description><![CDATA[Over on my other site (Salathe, just a single page site) I felt that keeping the page static was a little bit boring: it needed some life injected into it. That life, I decided, would be a summary of my recent blog posts on here. They don&#8217;t come all too often but I figured it [...]]]></description>
			<content:encoded><![CDATA[<p>Over on my other site (<a href="http://salathe.co.uk" rel="me org work">Salathe</a>, just a single page site) I felt that keeping the page static was a little bit boring: it needed some life injected into it. That life, I decided, would be a summary of my recent blog posts on here. They don&#8217;t come all too often but I figured it would be nice to syndicate them across there and maybe help people find out a bit more about me if they land on the Salathe site.<span id="more-99"></span></p>
<h3>Preamble</h3>
<p>I could&#8217;ve gone the server-side route; use PHP to grab the feed from here (well, Feedburner actually), parse it (probably with SimpleXML for simplicity) and display it with my desired HTML. And that&#8217;s a perfectly acceptable approach.  However, since the feed content is only really an added extra I didn&#8217;t want to add the feed content to the HTML page and have that overwhelm the main content on there (the introduction and contact info).  Because of that, I decided to take the client-side approach using JavaScript to grab, parse and inject our feed into the page.</p>
<p>The Feedburner feed, as is usual for feeds, comes in XML format and due to the security restrictions of browsers I can&#8217;t just go and grab that with JavaScript. Besides, I didn&#8217;t want the whole feed anyway: who wants to load in a 38kb feed just to grab some headings? (What I do end up grabbing is around 4kb.)</p>
<h3>Introducing YQL</h3>
<p>To grab only what I want, and to provide a format that I can actually get at, as well as to satisfy a niggling urge to put it into some practical use, I headed over to Yahooland and made use of the awesome tool that is <a href="http://developer.yahoo.com/yql/"><abbr title="Yahoo Query Language">YQL</abbr></a>.  YQL provides a SQL-like syntax for getting at the structured data (feeds are structured data!) around the web. It allows me to ask for specific items of data from the web, such as &#8220;give me the title and permalink for the last 10 items from Yahoo news&#8221; for example. There&#8217;s a huge range of data that YQL can get its hands on (stopping just short of the entire web) but that&#8217;s beyond the scope of this blog entry. The service also allows me to get responses in <abbr title="eXtensible Markup Language">XML</abbr> or <abbr title="JavaScript Object Notation with Padding">JSONP</abbr>, the latter being important because I <em>can</em> grab this from a remote domain name without the security restrictions coming into play.</p>
<h3>Querying for data</h3>
<p>So to get at my feed, I can simply issue the query:<br />
<a href="http://developer.yahoo.com/yql/console/?q=SELECT%20*%20FROM%20feed%20WHERE%20url%3D%27http%3A%2F%2Ffeeds2.feedburner.com%2Fcowburn%27" target="_blank" rel="nofollow">Try it yourself</a></p>

<div class="wp_syntax"><div class="code"><pre class="sql" ><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> feed <span style="color: #993333; font-weight: bold;">WHERE</span> url<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'http://feeds2.feedburner.com/cowburn'</span></pre></div></div>

<p>For my little widget, I don&#8217;t really need everything brought back by that query. Firstly, I only want the latest 5 items instead of the 10 that Feedburner provides. Secondly, I only really need the title, date, link and excerpt for each blog entry. The Feedburner feed provides extra information like the comments link, tags, and the full HTML content of the entry.  Grabbing only what I need shrinks down the size of the data coming down the pipe and results in things getting done faster.  I mentioned before that I wanted the results in JSONP format which is a simple enough task: just click the JSON radio button in the YQL console, or add &#8220;format=json&#038;callback=cbfunc&#8221; (where cbfunc is your callback function name) to the REST query URL.  So to grab only what I want, the following YQL query is used: <a href="http://developer.yahoo.com/yql/console/?q=SELECT%20title%2C%20description%2C%20link%2C%20pubDate%20FROM%20feed(0%2C5)%20WHERE%20url%3D%27http%3A%2F%2Ffeeds2.feedburner.com%2Fcowburn%27" target="_blank" rel="nofollow">Try it yourself</a></p>

<div class="wp_syntax"><div class="code"><pre class="sql" ><span style="color: #993333; font-weight: bold;">SELECT</span> title<span style="color: #66cc66;">,</span> description<span style="color: #66cc66;">,</span> link<span style="color: #66cc66;">,</span> pubDate 
<span style="color: #993333; font-weight: bold;">FROM</span> feed<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">WHERE</span> url<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'http://feeds2.feedburner.com/cowburn'</span></pre></div></div>

<p>Aside: the <code>(0,5)</code> limits YQL to the first 5 items in the Feedburner feed.</p>
<p>Right, now we have the data that we want and can get it in JSONP format. How do we go about doing that exactly? JavaScript time!</p>
<h3>Retrieving the JSONP with jQuery</h3>
<p>I really like using jQuery for adding in extra goodness to sites and I was already using it for a little piece of DOM manipulation on the page (interesting but not to be covered in this post).  Plus, jQuery makes grabbing JSONP content very, very simple indeed using its <code>jQuery.getJSON</code> (<a href="http://docs.jquery.com/Ajax/jQuery.getJSON">docs</a>) function.  Basically we provide that function with an URL to the JSON document and a callback function to execute once jQuery has grabbed it. See the <a href="http://docs.jquery.com/Ajax/jQuery.getJSON">docs</a> for examples.</p>
<h3>Creating a widget</h3>
<p>Generally, there&#8217;s a logical structure to creating a widget like this one.  Steps involve grabbing the required data, parsing it, building the HTML and attaching it to the page.  Since this would take an inordinately long time to explain step by step (perhaps some other intrepid blogger has already done it) I&#8217;ll just show you with code.  Below is the JavaScript—in jQuery plugin form though that isn&#8217;t particularly necessary or important (just means it&#8217;s all wrapped up neatly and only concerned with itself). There are a number of variables to hold HTML, the YQL URL and a number of functions which parse the JSON object into HTML form and a little bit of effects to fade in the HTML block when it&#8217;s all ready. There&#8217;s also a function to parse the dates from &#8220;Mon, 05 Jan 2009 16:28:34 +0000&#8243; into &#8220;5 Jan&#8221; form.</p>
<p>Put simply, the widget code just says, &#8220;when the page is loaded, ask YQL for the filtered feed, parse the response items into a HTML list and inject it into my page just above the footer.&#8221;</p>
<h3>The widget code</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" ><span style="color: #006600; font-style: italic;">// Grab food!</span>
<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>
	jQuery<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> wrapper <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id=&quot;widget_blog&quot;&gt;'</span>
		            <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;h4&gt;From the &lt;a href=&quot;http://cowburn.info&quot; rel=&quot;me&quot;&gt;blog&lt;/a&gt;:&lt;/h4&gt;'</span>
		            <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;ul&gt;&lt;/ul&gt;'</span>
		            <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> list    <span style="color: #339933;">=</span> wrapper.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul:first'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> style   <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;style type=&quot;text/css&quot;&gt;@import url(assets/feed.css);&lt;/style&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> service <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://query.yahooapis.com/v1/public/yql?q=select%20title%2C&quot;</span>
		            <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;description%2Clink%2CpubDate%20from%20feed(0%2C5)%20where%20url%3D'&quot;</span>
		            <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;http%3A%2F%2Ffeeds2.feedburner.com%2Fcowburn'&amp;format=json&amp;callback=?&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> callback <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<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: #339933;">!</span> <span style="color: #009900;">&#40;</span>data <span style="color: #339933;">&amp;&amp;</span> data.<span style="color: #660066;">query</span> <span style="color: #339933;">&amp;&amp;</span> data.<span style="color: #660066;">query</span>.<span style="color: #660066;">results</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>items <span style="color: #339933;">=</span> data.<span style="color: #660066;">query</span>.<span style="color: #660066;">results</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #006600; font-style: italic;">// Unexpected response from YQL service</span>
				<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Inject HTML before the footer</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.footer:first'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">before</span><span style="color: #009900;">&#40;</span>wrapper<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			wrapper.<span style="color: #660066;">before</span><span style="color: #009900;">&#40;</span>style<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>items<span style="color: #339933;">,</span> display_item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			wrapper.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slow&quot;</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: #003366; font-weight: bold;">var</span> parse_date <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> m <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Jan'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Feb'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Mar'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Apr'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'May'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'June'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'July'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Aug'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Sept'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Oct'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Nov'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Dec'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">return</span> d.<span style="color: #660066;">getUTCDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> m<span style="color: #009900;">&#91;</span>d.<span style="color: #660066;">getUTCMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> display_item <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> tpl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;li&gt;&lt;span&gt;%date: &lt;/span&gt;&lt;a href=&quot;%link&quot; title=&quot;%extract&quot;&gt;%title&lt;/a&gt;&lt;/li&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> tpl.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/%link/g</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">link</span><span style="color: #009900;">&#41;</span>
			              .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/%extract/g</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">description</span><span style="color: #009900;">&#41;</span>
			              .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/%title/g</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">title</span><span style="color: #009900;">&#41;</span>
			              .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/%date/g</span><span style="color: #339933;">,</span> parse_date<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">pubDate</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			list.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>html<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;">// Actually grab the feed now</span>
		$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span>service<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</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>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As well as the HTML to display the feed items, the widget also adds a CSS stylesheet to add a little bit of formatting: <a href="http://cowburn.info/files/yql/salathe-feed.css">link</a>. The widget code itself (which may change in the future) can be found <a href="http://cowburn.info/files/yql/salathe-feed.js">here</a>.</p>
<h3>End result</h3>
<p>What we&#8217;re left with is a neat little list of my last 5 blog entries.</p>
<div id="attachment_106" class="wp-caption aligncenter" style="width: 540px"><img src="http://cowburn.info/wp-content/uploads/2009/01/salathe-feed.png" alt="Salathe.co.uk before/after widget" title="Salathe Feed Widget" width="530" height="265" class="size-full wp-image-106" /><p class="wp-caption-text">Salathe.co.uk before/after widget</p></div>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/01/20/feed-widget-with-yql-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Case of the Disppearing Sidebar</title>
		<link>http://cowburn.info/2009/01/15/disppearing-sidebar/</link>
		<comments>http://cowburn.info/2009/01/15/disppearing-sidebar/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 14:46:56 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[sidebar]]></category>
		<category><![CDATA[ui]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=84</guid>
		<description><![CDATA[Howdy folks, as part of the ongoing little tweaks and changes that I make to this blog, I decided to try a little experiment in minimalism. The target of this experiment is my little sidebar which is attached to the right-hand side (at the moment) of every single page. The main point is that I [...]]]></description>
			<content:encoded><![CDATA[<p>Howdy folks, as part of the ongoing little tweaks and changes that I make to this blog, I decided to try a little experiment in minimalism.</p>
<p>The target of this experiment is my little sidebar which is attached to the right-hand side (at the moment) of every single page.  The main point is that I got sick of continually seeing this sidebar beside my main content: it&#8217;s distracting.</p>
<p><span id="more-84"></span></p>
<p>To put the emphasis back onto the content, I thought of a few things such as changing the colours (to a lighter shade) as well as text size in order to help the sidebar fade into the background. However, nothing really quite cut it.  The only thing that really seemed to work was to remove the sidebar completely!  The idea seemed silly since there are some useful links and tools in there (it&#8217;s surprising how many people want to translate my posts and there&#8217;s the neat little search box).  Could there be a compromise? Ideally, we want to see the sidebar when there&#8217;s something inside we want to make use of but at other times hide it away to keep the page tidy.  Hovering over the area where the sidebar is (as you would if you wanted to use something inside it) proved to be the simplest trigger.</p>
<p>But if the sidebar is hidden, how to people know it&#8217;s there? I thought of having a little button there which provided a visual cue when the sidebar was hidden, but that went against the idea of hiding it in the first place since there was always something there, being distracting.</p>
<p>Instead I decided to display the sidebar for a couple of seconds when the page is loaded and then to fade it out automatically.  This visual change in the page is enough for the eye to recognise the change (in this case the block fading out) in the peripheral vision so that the visitor knows there&#8217;s a sidebar but it&#8217;s just hiding at the moment.  That&#8217;s the idea anyway.</p>
<div id="attachment_86" class="wp-caption aligncenter" style="width: 530px"><img src="http://cowburn.info/wp-content/uploads/2009/01/sidebar.gif" alt="Before/after showing sidebar" title="Disappearing Sidebar" width="530" height="166" class="size-full wp-image-86" /><p class="wp-caption-text">Before <span class="amp">&#038;</span> after showing sidebar</p></div>
<p>What are your thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/01/15/disppearing-sidebar/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Comments on Simple AJAX with jQuery</title>
		<link>http://cowburn.info/2008/03/10/comments-on-simple-ajax-with-jquery/</link>
		<comments>http://cowburn.info/2008/03/10/comments-on-simple-ajax-with-jquery/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 23:04:49 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[gareth]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[talkphp]]></category>

		<guid isPermaLink="false">http://cowburn.info/js/comments-on-simple-ajax-with-jquery/</guid>
		<description><![CDATA[Today, a new article was approved over on TalkPHP entitled &#8220;Simple AJAX with JQuery&#8221; (read article). If you&#8217;re looking for a nice basic introduction to using jQuery to make AJAX calls, then head on over there. Here are some thoughts on it. The main chunk of JavaScript code used in the article is as follows: [...]]]></description>
			<content:encoded><![CDATA[<p>Today, a new article was approved over on <a href="http://www.talkphp.com/">TalkPHP</a> entitled &#8220;Simple AJAX with JQuery&#8221; (<a href="http://www.talkphp.com/vbarticles.php?do=article&#038;articleid=58&#038;title=simple-ajax-with-jquery">read article</a>).  If you&#8217;re looking for a nice basic introduction to using jQuery to make AJAX calls, then head on over there.  Here are some thoughts on it. </p>
<p><span id="more-25"></span>The main chunk of JavaScript code used in the article is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" ><span style="color: #003366; font-weight: bold;">function</span> register<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #339933;">,</span>
		url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;submit_data.php&quot;</span><span style="color: #339933;">,</span>
		data<span style="color: #339933;">:</span> 	<span style="color: #3366CC;">&quot;username=&quot;</span> <span style="color: #339933;">+</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;username&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">+</span> 
			<span style="color: #3366CC;">&quot;&amp;email=&quot;</span> <span style="color: #339933;">+</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">,</span>
		success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#response&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>html<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></div></div>

<p>Whilst that works, there are a number of things that <em>I</em> would do differently (that doesn&#8217;t make either of the ways right, or wrong, necessarily!).  My version of the AJAX call would probably move further in the direction of:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" >$.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;submit_data.php&quot;</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form:first&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">serializeArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#response&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>How&#8217;s this different? Well first, we make use of the <code>jQuery.post</code> function rather than using the lower level <code>jQuery.ajax</code> with POST specified as an argument.  There is an equally useful <code>jQuery.get</code> option if we wanted to use a GET request instead.  Note the <code>form:first</code> selector, obviously that would only grab the data for the first form on the page but since there is only one form in the example HTML we&#8217;re ok. </p>
<p>The next major change is rather than construct the data string manually as Gareth did in the article (why use <code>document.getElementById</code> rather than jQuery&#8217;s dollar functon?) we make use of the <code>jQuery.serializeArray</code> function which serialises the form elements (if any) of an element into an object.  The conversion of that object into a usable data string is done within jQuery and we needn&#8217;t construct our own.  I&#8217;d use <code>serializeArray</code> over <code>serialize</code> simply because I often want to include more data items in the request using <code>jQuery.extend</code>.</p>
<p>A few other points to make note of, which I think Gareth will include in a follow-up article, would be that no effort is made to make the article&#8217;s sample code <em>unobtrusive</em> (which is all the rage right now) and without JavaScript enabled the newsletter signup simply would not work at all. </p>
<p>Related to this, he relies on the <code>click</code> event of the button to trigger the request. This is something I see time and time again with AJAX form submission, maybe everyone else in the world clicks submit buttons but I generally press the <em>enter key</em> (I&#8217;m much more keyboard oriented when browsing, than pointer oriented [mouse, trackpad, etc]).  Binding to the form&#8217;s <code>submit</code> event (unobtrusively, not within the HTML) would be my recommendation since we really want to hijack the normal submission process and route it through AJAX.</p>
<p>Hmm well, there we are. It&#8217;s not a very coherent set of thoughts but they&#8217;re out there now. <img src='http://cowburn.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2008/03/10/comments-on-simple-ajax-with-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
