<?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>Jeremy Smyth's Blog &#187; ajax</title>
	<atom:link href="http://jeremysmyth.com/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeremysmyth.com</link>
	<description></description>
	<lastBuildDate>Sun, 04 Jul 2010 07:10:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>&#8216;Hello World&#8217; using AJAX</title>
		<link>http://jeremysmyth.com/2009/08/20/hello-world-using-ajax/</link>
		<comments>http://jeremysmyth.com/2009/08/20/hello-world-using-ajax/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 14:16:54 +0000</pubDate>
		<dc:creator>Jeremy Smyth</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jeremysmyth.com/?p=53</guid>
		<description><![CDATA[&#8220;Asynchronous Javascript And XML&#8221; is a mouthful for a web technology that&#8217;s in wide use these days. Simply put, it lets a web page download more content without reloading the page, and relies on a magic Javascript object called an XmlHttpRequest to do the work of requesting more data in the background, so it can [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Asynchronous Javascript And XML&#8221; is a mouthful for a web technology that&#8217;s in wide use these days. Simply put, it lets a web page download more content without reloading the page, and relies on a magic Javascript object called an XmlHttpRequest to do the work of requesting more data in the background, so it can fill in things on the page.</p>
<p>So, fiddling with AJAX, let&#8217;s do a little Hello Worlding:</p>
<p><script type='text/javascript'>
<!--
var xhr;
if (window.XMLHttpRequest){
 xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject){ // in case it's IE
 xhr = new ActiveXObject("Microsoft.XMLHTTP");
 }
// set up the callback
xhr.onreadystatechange = function(){
 myDiv = document.getElementById("ajaxtest");
 if(xhr.readyState  == 4) {
 if(xhr.status  == 200){
 myDiv.style.background = "green";
 myDiv.innerHTML =  xhr.responseText;
 } else {
 myDiv.innerHTML = "Error: " + xhr.status;
 }
 }  
}
function startRequest(){
 xhr.open('GET', '/ajaxtest.php', true);
 xhr.send(null);
}
-->
</script></p>
<p>Here&#8217;s a button, that when clicked, calls the &#8220;startRequest()&#8221; function in some javascript behind the entry:</p>
<pre>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/input.html"><span class="kw2">input</span></a> <span class="kw3">onclick</span><span class="sy0">=</span><span class="st0">&quot;startRequest()&quot;</span> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;button&quot;</span> <span class="kw3">value</span><span class="sy0">=</span><span class="st0">&quot;Hello World!&quot;</span> <span class="sy0">/</span>&gt;</span></div>
</div>
</pre>
<input onclick="startRequest()" type="button" value="Hello World!" />
<p />
<p>And here&#8217;s a div:</p>
<pre>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/div.html"><span class="kw2">div</span></a> <span class="kw3">id</span><span class="sy0">=</span><span class="st0">&quot;ajaxtest&quot;</span>&gt;</span>
<span class="sc2">&lt;<a href="http://december.com/html/4/element/em.html"><span class="kw2">em</span></a>&gt;</span>(although you can't see it as a separate div, 
this text will be replaced)<span class="sc2">&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/em.html"><span class="kw2">em</span></a>&gt;&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/div.html"><span class="kw2">div</span></a>&gt;</span></div>
</div>
</pre>
<div id="ajaxtest"><em>(although you can&#8217;t see it as a separate div, this text will be replaced)</em></div>
<p />
<p>Go on, watch the div text above, and click the button.</p>
<p>Here&#8217;s the javascript:</p>
<pre>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="kw2">var</span> xhr<span class="sy0">;</span>
<span class="kw1">if</span> <span class="br0">&#40;</span>window.<span class="me1">XMLHttpRequest</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; xhr <span class="sy0">=</span> <span class="kw2">new</span> XMLHttpRequest<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>window.<span class="me1">ActiveXObject</span><span class="br0">&#41;</span><span class="br0">&#123;</span> <span class="co1">// in case it's IE</span>
&nbsp; xhr <span class="sy0">=</span> <span class="kw2">new</span> ActiveXObject<span class="br0">&#40;</span><span class="st0">&quot;Microsoft.XMLHTTP&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;<span class="br0">&#125;</span>
<span class="co1">// set up the callback</span>
xhr.<span class="me1">onreadystatechange</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; myDiv <span class="sy0">=</span> document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&quot;ajaxtest&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>xhr.<span class="me1">readyState</span> &nbsp;<span class="sy0">==</span> 4<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>xhr.<span class="kw3">status</span> &nbsp;<span class="sy0">==</span> <span class="nu0">200</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; &nbsp;myDiv.<span class="me1">style</span>.<span class="me1">background</span> <span class="sy0">=</span> <span class="st0">&quot;green&quot;</span><span class="sy0">;</span>
&nbsp; &nbsp;myDiv.<span class="me1">innerHTML</span> <span class="sy0">=</span> &nbsp;xhr.<span class="me1">responseText</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
&nbsp; &nbsp;myDiv.<span class="me1">innerHTML</span> <span class="sy0">=</span> <span class="st0">&quot;Error: &quot;</span> <span class="sy0">+</span> xhr.<span class="kw3">status</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="br0">&#125;</span> &nbsp;
<span class="br0">&#125;</span>
<span class="kw2">function</span> startRequest<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp; xhr.<span class="kw3">open</span><span class="br0">&#40;</span><span class="st0">'GET'</span><span class="sy0">,</span> <span class="st0">'/ajaxtest.php'</span><span class="sy0">,</span> <span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; xhr.<span class="me1">send</span><span class="br0">&#40;</span><span class="kw2">null</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p />
<p />Firstly, the code sets up a variable <tt>xhr</tt>, which contains the magical object. This is the <tt>XmlHttpRequest</tt> object, and is responsible for communicating from the live Javascript to the back end server.</p>
<p>The request object is first set up at the script&#8217;s execution, but the only thing we do with it is tell it what to do when its &#8220;ready state&#8221; changes; that is, when the request shifts from not initialised, to set up, to having been sent, to being in process, to completion. </p>
<p>These states are numbered 0-4 respectively. Hold that thought; we&#8217;ll use it later. Suffice to say that, on the script&#8217;s execution, we now have an otherwise anonymous function that will be called when the ready state changes.</p>
<p>Now, in order for something interesting to happen, we click the button. That kicks off <tt>startRequest()</tt>, which in turn sets up a asynchronous request to <tt>ajaxtest.php</tt>, and submits it with no parameters (that&#8217;s the <tt>null</tt>). Asynchronous means we don&#8217;t sit around waiting for a response, and that&#8217;s why we need the readystatechange callback function.</p>
<p>So, when something interesting happens to the xhr object, the ready state changes (e.g. when we get a response back after sending our request). The browser then executes the anonymous function: first, the function checks that the request is complete &#8211; remember Ready State 4 above? don&#8217;t forget the ready state changes several times per request. We only want to read the response text once, at completion. Once the function has checked the request has been successfully completed,  it then uses the xhr&#8217;s response-text to fill in the div called &#8220;ajaxtest&#8221;. </p>
<p>Et voila, we have &#8220;Hello World&#8221; in AJAX.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeremysmyth.com/2009/08/20/hello-world-using-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
