<?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>PlanetB &#187; E4X</title>
	<atom:link href="http://www.planetb.ca/tag/e4x/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.planetb.ca</link>
	<description></description>
	<lastBuildDate>Sat, 31 Dec 2011 23:31:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Ajax Made Easy &#8211; the Power of jQuery</title>
		<link>http://www.planetb.ca/2008/11/ajax-made-easy-the-power-of-jquery/</link>
		<comments>http://www.planetb.ca/2008/11/ajax-made-easy-the-power-of-jquery/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 03:56:01 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[E4X]]></category>
		<category><![CDATA[ECMAScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xmlhttprequest]]></category>

		<guid isPermaLink="false">http://www.planetb.ca/?p=71</guid>
		<description><![CDATA[Ajax &#8211; Asynchronous Javascript and XML. It&#8217;s more a technique than anything else. While senior management may consider Ajax to be fancy animations and &#8220;rich&#8221; internet applications, fundamentally, the term Ajax simply refers to exploiting a browser&#8217;s ability to background load XML from other URL&#8217;s. Loading XML Asynchronously In the past, I&#8217;ve implemented Ajaxiness by [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:15pt;font-weight:bold;">Ajax</span> &#8211; <strong>A</strong>synchronous <strong>Ja</strong>vascript and <strong>X</strong>ML.  It&#8217;s more a technique than anything else.  While senior management may consider Ajax to be fancy animations and &#8220;rich&#8221; internet applications, fundamentally, the term Ajax simply refers to exploiting a browser&#8217;s ability to background load XML from other URL&#8217;s.</p>
<h2>Loading XML Asynchronously</h2>
<p>In the past, I&#8217;ve implemented Ajaxiness by creating XMLHttpRequest objects (or Microsoft.XMLHTTP objects fo IE):</p>
<pre class="brush: javascript">
var request = (document.all)?new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;):new XMLHttpRequest();
request.onreadystatechange = function(){
	if(request.readyState == 4)
		alert(&quot;Document loaded&quot;);
};
request.open(&quot;GET&quot;, &quot;test.xml&quot;, true);
request.send(&quot;&quot;);
</pre>
<p>The above code just loads an xml file, test.xml, and alerts the user when the load is finished.  It&#8217;s as simple an Ajax example as it gets.  Fully cross-browser compliant (as much as necessary anyway). From there, you could use your browser&#8217;s ridiculous XML parsing capabilities which isn&#8217;t the same between browsers and with much trial and error and a whack of frustration, implement some client-server Ajax and maybe pull data into a grid or something.  </p>
<p>As I&#8217;ve said, I&#8217;ve done it this way.  But it&#8217;s insane.  In this day and age of the wheel already having been invented over and over, the time needed to implement the core cross-browser Ajax functions should be zero.  In fact, the addition of a particular library to your script can not only make cross browser Ajax and DOM\XML processing worry free, it can make your scripting days a hell of a lot easier in general.</p>
<p>That incredible library is&#8230; <span style="font-size:20pt;font-weight:bold;">jQuery</span>.</p>
<p>With jQuery, the above code becomes a one liner (sorta anyway):</p>
<pre class="brush: javascript">
$.ajax({
  url: &quot;test.xml&quot;,
  success: function(retxml){
    alert(&quot;Document loaded&quot;);
  }
});
</pre>
<h2>ECMAScript for XML (E4X)</h2>
<p>ECMAScript is javascript.  They&#8217;re one and the same.  In 2004, a standardized extended version of ECMAScript was published that elegantly treated xml as a primitive data type.  Doing so makes traversing an XML or DOM document a breeze.  Firefox and Safari both support this standard.  So does Adobe Flash\Flex actionscript.  Unfortunately (and unsurprisingly) IE doesn&#8217;t and from what I&#8217;ve read, MS doesn&#8217;t intend to change that.   This being the case, combined with the utterly depressing fact that IE is so incredibly popular, means that using E4X in any script is likely a poor choice for the time being (unless, of course, you choose not to support IE &#8211; good luck with that).</p>
<h2>jQuery &#8211; the E4X Alternative</h2>
<p>So for now, until either Dell&#8217;s stop being shipped with IE as the default browser or Microsoft decides to incorporate more of the browser standards, we&#8217;re squeezed out from using xml as a primitive data type.  While this no doubt sucks, <b>jQuery</b> again comes to the rescue.</p>
<p>Suppose the following XML file named test.xml :</p>
<pre class="brush: xml">
&lt;Provinces&gt;
	&lt;Province name=&quot;Ontario&quot;&gt;
		&lt;Cities&gt;
			&lt;City&gt;Kitchener&lt;/City&gt;
			&lt;City&gt;Guelph&lt;/City&gt;
			&lt;City&gt;Toronto&lt;/City&gt;
		&lt;/Cities&gt;
	&lt;/Province&gt;
&lt;/Provinces&gt;
</pre>
<p>We can easily load the file and traverse all City elements as follows:</p>
<pre class="brush: javascript">
$.ajax({
  url: &quot;test.xml&quot;,
  success: function(retxml){
    $(retxml).find(&quot;City&quot;).each(function(){
    	alert(&quot;City: &quot; + $(this).text());
    })
  }
});
</pre>
<p>Easy peasy&#8230;</p>
<p>jQuery not only enables easy Ajax, it also has many other cool features like fading and sliding elements and animation support that would certainly feed management&#8217;s buzz word frenzy.</p>
<p>For more information about jQuery, check out the homepage, <a href="http://jquery.com/">http://jquery.com/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.planetb.ca/2008/11/ajax-made-easy-the-power-of-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

