<?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>jeremykendall.net &#187; javascript</title>
	<atom:link href="http://www.jeremykendall.net/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeremykendall.net</link>
	<description>{web developer, entrepreneur }</description>
	<lastBuildDate>Fri, 23 Jul 2010 19:18:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zebra Tables with jQuery</title>
		<link>http://www.jeremykendall.net/2008/12/11/zebra-tables-with-jquery/</link>
		<comments>http://www.jeremykendall.net/2008/12/11/zebra-tables-with-jquery/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 13:45:33 +0000</pubDate>
		<dc:creator>Jeremy Kendall</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.jeremykendall.net/?p=120</guid>
		<description><![CDATA[I&#8217;ve used the classic A List Apart Zebra Tables technique to stripe my tables for years.  It&#8217;s always worked well, and I never really considered updating the technique until last week.  I&#8217;ve been making heavy use of the jQuery library lately, and I really disliked including another external js file whenever I wanted to stripe [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve used the classic A List Apart <a href="http://www.alistapart.com/articles/zebratables"><em>Zebra Tables</em></a> technique to stripe my tables for years.  It&#8217;s always worked well, and I never really considered updating the technique until last week.  I&#8217;ve been making heavy use of the <a href="http://jquery.com/">jQuery</a> library lately, and I really disliked including another external js file whenever I wanted to stripe a table, so I thought I&#8217;d see if someone had come up with a jQuery friendly table striping technique.  It took about 10 seconds on Google to find what I was looking for, and the solution was so simple and elegant that I wanted to kick myself for not thinking of it, er, myself.</p>
<p>For the whole scoop, head over and <a href="http://15daysofjquery.com/examples/zebra/">read the tutorial</a>.  If you&#8217;re like me, you want to get right to the point, so here goes.</p>
<p>The idea is to use jQuery to select alternate rows from your table and apply a css class to them. In the example below, the table has a class of &#8217;striped&#8217; and jQuery adds the class &#8216;alt&#8217; to the even rows.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</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>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.striped tr:even&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;alt&quot;</span><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>Whip up a little css that adds a background color to .alt and you&#8217;re done.  Not bad, huh?</p>
<p>The tutorial author also included an example of jQuery code that allows for a nice hover effect when you mouse over the table rows.  I wasn&#8217;t as interested in that, so you&#8217;ll have to <a href="http://15daysofjquery.com/examples/zebra/">head over there</a> for the scoop.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremykendall.net/2008/12/11/zebra-tables-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic Content on Static Pages using Zend Json and jQuery</title>
		<link>http://www.jeremykendall.net/2008/10/16/dynamic-content-on-static-pages-using-zend-json-and-jquery/</link>
		<comments>http://www.jeremykendall.net/2008/10/16/dynamic-content-on-static-pages-using-zend-json-and-jquery/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 20:48:17 +0000</pubDate>
		<dc:creator>Jeremy Kendall</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.jeremykendall.net/?p=59</guid>
		<description><![CDATA[The Problem
One of the most highly trafficked sites on the intranet where I work is the cafeteria&#8217;s web site.  Employees from all across the enterprise visit daily to see what&#8217;s on the menu.  Maintaining the menu pages on the cafeteria site has been a constant challenge for our content folks.
As the bulk of our intranet [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Problem</strong></p>
<p>One of the most highly trafficked sites on the intranet where I work is the cafeteria&#8217;s web site.  Employees from all across the enterprise visit daily to see what&#8217;s on the menu.  Maintaining the menu pages on the cafeteria site has been a constant challenge for our content folks.</p>
<p>As the bulk of our intranet is still mostly static (PHP isn&#8217;t even installed on our main intranet box), our content people have to work really hard to keep the menu up-to-date.  The process consists of the cafeteria folks emailing an updated menu over to our content people and our content folks manually updating the menu&#8217;s html.  Since the cafeteria is open seven days a week, that means our content folks have to manage eight separate pages: the cafeteria homepage featuring the current day&#8217;s menu in a side bar and separate menu pages for each day of the week.</p>
<p><strong>The Solution, Almost</strong></p>
<p>The solution to this issue began with a simple Java CRUD app, allowing the cafeteria employees to manage the menu themselves.  The &#8220;Menu Builder&#8221; application spits out an XML document for consumption on the cafeteria website.  During the development process, I extended <a title="Zend Http" href="http://framework.zend.com/manual/en/zend.http.html">Zend_Http_Client</a> and wrote a simple index page to parse the XML for display.  While the PHP solution for displaying the menu was nice, clean, and simple, it turned out to be completely useless.</p>
<p>As I mentioned above, the majority of our intranet is still running on a server without PHP installed.  We&#8217;re working to move away from that server, but the process is slow and painful.  Without the ability to use PHP to serve the daily menu, we had to come up with something else.  AJAX was the obvious choice, but how to deal with the fact that we&#8217;d have to make cross-domain requests in order to retrieve the application&#8217;s XML output?</p>
<p><strong>Cross-Domain JSON with jQuery</strong></p>
<p>After a little digging around, one of my co-workers turned me on to <a title="jQuery" href="http://jquery.com/">jQuery</a> and <a title="JSON with Padding" href="http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/">JSONP</a>.  As of version 1.2, jQuery provides native support for JSONP, a method of retrieving JSON data across domains.  The implementation can be found in <a title="getJSON" href="http://docs.jquery.com/Ajax/jQuery.getJSON">jQuery.getJSON</a>.</p>
<p>Once the decision was made to utilize jQuery&#8217;s getJSON, I had to come up with a way to convert the menu&#8217;s XML output to JSON.  Since the Menu Builder app was already spitting out XML, there was no way I was going to add a module to the application to support the new JSON requirement.  I needed a simple way to convert XML to JSON, and I didn&#8217;t want to spend any time rolling my own solution.</p>
<p><strong>Zend Framework to the Rescue</strong></p>
<p>Since we&#8217;re already using <a title="Zend Framework" href="http://framework.zend.com/">Zend Framework</a> on our intranet, I decided to look to <a title="Zend Json" href="http://framework.zend.com/manual/en/zend.json.html">Zend Json</a> and see if it could do the job.  Sure enough, there&#8217;s a static <a title="Zend_Json::fromXml()" href="http://framework.zend.com/manual/en/zend.json.xml2json.html">fromXml()</a> method that takes care of the conversion beautifully.</p>
<p>After that, it was fairly quick work to create some JavaScript functions to parse the returned JSON and display it on the cafeteria website.</p>
<p>[Side note: As of ZF 1.6, the <a title="Dojo" href="http://dojotoolkit.org/">Dojo</a> JavaScript library is <a title="Zend Dojo" href="http://framework.zend.com/manual/en/zend.dojo.html">integrated with Zend Framework</a>.  Well before that integration occurred, we made the decision that jQuery would be our library of choice.  That's why I'm using jQuery for this solution rather than Dojo.]</p>
<p><strong>The Code</strong></p>
<p>First is an example of the XML returned from the Menu Builder application.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;timeStamp<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Thu Oct 16 13:18:50 CDT 2008<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/timeStamp<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;day<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Thursday<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/day<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;date<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>October 16, 2008<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/date<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meal</span> <span style="color: #000066;">descr</span>=<span style="color: #ff0000;">&quot;Breakfast Feature&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">descr</span>=<span style="color: #ff0000;">&quot;None (will not display to users)&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;itemdescr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>French Toast<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/itemdescr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;itemprice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.45<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/itemprice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;itemdescr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Biscuit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/itemdescr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;itemprice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.25<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/itemprice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/category<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/meal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meal</span> <span style="color: #000066;">descr</span>=<span style="color: #ff0000;">&quot;Lunch&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">descr</span>=<span style="color: #ff0000;">&quot;Grab &amp;amp; Go&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;itemdescr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Garden Vegetables<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/itemdescr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;itemprice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.89<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/itemprice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/category<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">descr</span>=<span style="color: #ff0000;">&quot;Papa J's Pizza by the Slice&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;itemdescr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Sausage Egg <span style="color: #ddbb00;">&amp;amp;</span> Cheese Biscuit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/itemdescr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;itemprice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.09<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/itemprice<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/category<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/meal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Running the above XML through Zend_Json::fromXml() provided me with the JSON representation I needed.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;menu&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;timeStamp&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Thu Oct 16 13:18:50 CDT 2008&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;day&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Thursday&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;date&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;October 16, 2008&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;meal&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;@attributes&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #3366CC;">&quot;descr&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Breakfast Feature&quot;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;category&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #3366CC;">&quot;@attributes&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #3366CC;">&quot;descr&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;None (will not display to users)&quot;</span>
                    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #3366CC;">&quot;item&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                        <span style="color: #009900;">&#123;</span>
                            <span style="color: #3366CC;">&quot;itemdescr&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;French Toast&quot;</span><span style="color: #339933;">,</span>
                            <span style="color: #3366CC;">&quot;itemprice&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0.45&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #009900;">&#123;</span>
                            <span style="color: #3366CC;">&quot;itemdescr&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Biscuit&quot;</span><span style="color: #339933;">,</span>
                            <span style="color: #3366CC;">&quot;itemprice&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0.25&quot;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#93;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;@attributes&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #3366CC;">&quot;descr&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Lunch&quot;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                <span style="color: #3366CC;">&quot;category&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #3366CC;">&quot;@attributes&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #3366CC;">&quot;descr&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Grab &amp;amp; Go&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;item&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #3366CC;">&quot;itemdescr&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Garden Vegetables&quot;</span><span style="color: #339933;">,</span>
                            <span style="color: #3366CC;">&quot;itemprice&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0.89&quot;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #3366CC;">&quot;@attributes&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #3366CC;">&quot;descr&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Papa J's Pizza by the Slice&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;item&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                            <span style="color: #3366CC;">&quot;itemdescr&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Sausage Egg &amp;amp; Cheese Biscuit&quot;</span><span style="color: #339933;">,</span>
                            <span style="color: #3366CC;">&quot;itemprice&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;2.09&quot;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#93;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see in the samples above, menu items belong to categories and categories belong to meals.  Both meals and categories are described in XML attributes rather than in a description node.</p>
<p>Next is the PHP script that I wrote to get the menu XML, convert it to JSON, and echo it out to the jQuery.getJSON call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Include required files</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/cafeteria/lib/base.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set service uri</span>
<span style="color: #000088;">$uri</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$cafeteriaConfig</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">menu</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">serviceuri</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Valid days of the week</span>
<span style="color: #000088;">$validDays</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'monday'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'tuesday'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wednesday'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thursday'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'friday'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'saturday'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sunday'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get day from querystring, validate</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'day'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000088;">$getDay</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'day'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$getDay</span><span style="color: #339933;">,</span> <span style="color: #000088;">$validDays</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$day</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$getDay</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$uri</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$uri</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'?day='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$day</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get xml from cafeteria service</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uri</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Encode returned xml as JSON, do not ignore XML attributes</span>
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> Zend_Json<span style="color: #339933;">::</span><span style="color: #004000;">fromXml</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Return response, wrapping response in the requested callback</span>
<span style="color: #666666; font-style: italic;">// @see http://remysharp.com/2007/10/08/what-is-jsonp/</span>
<span style="color: #666666; font-style: italic;">// @see http://docs.jquery.com/Release:jQuery_1.2/Ajax</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'('</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$json</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">')'</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are a couple of important things to note in the script above.</p>
<p>First, we keep a lot of code in a base.php file.  This includes <a title="Zend_Loader::registerAutoload()" href="http://framework.zend.com/manual/en/zend.loader.html#zend.loader.load.autoload">Zend_Loader::registerAutoload()</a> and the creation of a <a title="Zend Config" href="http://framework.zend.com/manual/en/zend.config.html">Zend_Config</a> object, among other things, allowing for much <a title="Don't repeat yourself" href="http://en.wikipedia.org/wiki/DRY_code">DRY</a>er code.</p>
<p>Second, you&#8217;ll notice that I&#8217;m using the second, optional parameter for fromXml().  Set to false, fromXml() will return a representation of the XML attributes present in the input.  fromXml() ignores XML attributes by default.</p>
<p>Finally, you can see how the callback name and parentheses are wrapped around the returned JSON before outputting anything back to jQuery.getJSON.  This is required.  Without it, your cross-domain request won&#8217;t return anything at all.</p>
<p>The next bit of code is the JavaScript that parses the returned JSON and outputs the full menu to the web.  Notice that there are two output functions.  This is to allow for two different styles of menu tables: one for the cafeteria homepage (sidebar), and one for the daily menu page (full sized).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Parses JSON representation of cafeteria menu and returns Menu table</span>
<span style="color: #006600; font-style: italic;">// Requires jQuery</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> menu <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Builds sidebar menu for cafeteria home page</span>
<span style="color: #003366; font-weight: bold;">function</span> outputSidebarMenu<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> day <span style="color: #339933;">=</span> json.<span style="color: #660066;">menu</span>.<span style="color: #660066;">day</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> heading <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;h4&gt;&lt;a href=&quot;'</span> <span style="color: #339933;">+</span> menuLink <span style="color: #339933;">+</span> <span style="color: #3366CC;">'?requestedDay='</span> <span style="color: #339933;">+</span> day.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> day <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'s Menu&lt;/a&gt;&lt;/h4&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	menu <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;table id=&quot;side_datatable&quot;&gt;'</span> <span style="color: #339933;">+</span> getMeals<span style="color: #009900;">&#40;</span>json.<span style="color: #660066;">menu</span>.<span style="color: #660066;">meal</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/table&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> timeStampComment<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> heading <span style="color: #339933;">+</span> menu<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Builds table for daily menu page</span>
<span style="color: #003366; font-weight: bold;">function</span> outputDailyMenu<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> day <span style="color: #339933;">=</span> json.<span style="color: #660066;">menu</span>.<span style="color: #660066;">day</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> json.<span style="color: #660066;">menu</span>.<span style="color: #660066;">date</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> meal <span style="color: #339933;">=</span> json.<span style="color: #660066;">menu</span>.<span style="color: #660066;">meal</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> heading <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;h4 style=&quot;text-align:right; font-weight:normal;&quot;&gt;&lt;span style=&quot;float:left; font-weight:bold;&quot;&gt;'</span> <span style="color: #339933;">+</span> day <span style="color: #339933;">+</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\'</span>s Menu&lt;/span&gt;'</span> <span style="color: #339933;">+</span> date <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/h4&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	menu <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;table class=&quot;datatable&quot; width=&quot;400&quot;&gt;'</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">+</span> getMeals<span style="color: #009900;">&#40;</span>meal<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/table&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> timeStampComment<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> heading <span style="color: #339933;">+</span> menu<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Get menu meals</span>
<span style="color: #003366; font-weight: bold;">function</span> getMeals<span style="color: #009900;">&#40;</span>meals<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> mealList <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>meals <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><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>meals <span style="color: #000066; font-weight: bold;">instanceof</span> Array<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>meals<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>meal<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				mealList <span style="color: #339933;">+=</span> buildMealRow<span style="color: #009900;">&#40;</span>meal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				mealList <span style="color: #339933;">+=</span> getCategories<span style="color: #009900;">&#40;</span>meal.<span style="color: #660066;">category</span><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>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			mealList <span style="color: #339933;">+=</span> buildMealRow<span style="color: #009900;">&#40;</span>meals<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			mealList <span style="color: #339933;">+=</span> getCategories<span style="color: #009900;">&#40;</span>meals.<span style="color: #660066;">category</span><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: #000066; font-weight: bold;">return</span> mealList<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Get meal's categories</span>
<span style="color: #003366; font-weight: bold;">function</span> getCategories<span style="color: #009900;">&#40;</span>categories<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> catList <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>categories <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Is categories an array?</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>categories <span style="color: #000066; font-weight: bold;">instanceof</span> Array<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>categories<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> category<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				catList <span style="color: #339933;">+=</span> buildCategoryRow<span style="color: #009900;">&#40;</span>category<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			    catList <span style="color: #339933;">+=</span> getItems<span style="color: #009900;">&#40;</span>category.<span style="color: #000066; font-weight: bold;">item</span><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>  
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			catList <span style="color: #339933;">+=</span> buildCategoryRow<span style="color: #009900;">&#40;</span>categories<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			catList <span style="color: #339933;">+=</span> getItems<span style="color: #009900;">&#40;</span>categories.<span style="color: #000066; font-weight: bold;">item</span><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: #000066; font-weight: bold;">return</span> catList<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Get category's items</span>
<span style="color: #003366; font-weight: bold;">function</span> getItems<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> itemList <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
	<span style="color: #006600; font-style: italic;">// Is items an array?</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>items <span style="color: #000066; font-weight: bold;">instanceof</span> Array<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>items<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<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>
			itemList <span style="color: #339933;">+=</span> buildItemRow<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><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>  
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
		itemList <span style="color: #339933;">+=</span> buildItemRow<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> itemList<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Build individual meal's table row</span>
<span style="color: #003366; font-weight: bold;">function</span> buildMealRow<span style="color: #009900;">&#40;</span>meal<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> descr <span style="color: #339933;">=</span> meal<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'@attributes'</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">descr</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> mealRow <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;tr&gt;&lt;th colspan=&quot;2&quot;&gt;'</span> <span style="color: #339933;">+</span> descr <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/th&gt;&lt;/tr&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> mealRow<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Build individual category's table row</span>
<span style="color: #003366; font-weight: bold;">function</span> buildCategoryRow<span style="color: #009900;">&#40;</span>category<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> catRow <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>category <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> descr <span style="color: #339933;">=</span> category<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'@attributes'</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">descr</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// Do not display the 'None' category</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>descr.<span style="color: #660066;">search</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/None/</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			catRow <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;tr class=&quot;title&quot;&gt;&lt;td colspan=&quot;2&quot;&gt;'</span> <span style="color: #339933;">+</span> descr <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/td&gt;&lt;/tr&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> catRow<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Build individual item's table row</span>
<span style="color: #003366; font-weight: bold;">function</span> buildItemRow<span style="color: #009900;">&#40;</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> itemRow <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		<span style="color: #003366; font-weight: bold;">var</span> itemDescr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">itemdescr</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> itemPrice <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">itemprice</span><span style="color: #339933;">;</span>
		itemRow <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;tr&gt;&lt;td&gt;'</span> <span style="color: #339933;">+</span> itemDescr <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/td&gt;&lt;td&gt;$'</span> <span style="color: #339933;">+</span> itemPrice <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/td&gt;&lt;/tr&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> itemRow<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Update Cafeteria Daily Menu page title with &quot;[day]'s Menu&quot;</span>
<span style="color: #003366; font-weight: bold;">function</span> updatePageTitle<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Get day to use in page title</span>
    <span style="color: #003366; font-weight: bold;">var</span> titleDay <span style="color: #339933;">=</span> json.<span style="color: #660066;">menu</span>.<span style="color: #660066;">day</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Append day to page title</span>
    document.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">title</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' | '</span> <span style="color: #339933;">+</span> titleDay <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'s Menu&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Output html comment with note on when menu was last cached for troubleshooting</span>
<span style="color: #006600; font-style: italic;">// and debugging</span>
<span style="color: #003366; font-weight: bold;">function</span> timeStampComment<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	  <span style="color: #003366; font-weight: bold;">var</span> day <span style="color: #339933;">=</span> json.<span style="color: #660066;">menu</span>.<span style="color: #660066;">day</span><span style="color: #339933;">;</span>
	  <span style="color: #003366; font-weight: bold;">var</span> cacheTimeStamp <span style="color: #339933;">=</span> json.<span style="color: #660066;">menu</span>.<span style="color: #660066;">timeStamp</span><span style="color: #339933;">;</span>
	  <span style="color: #003366; font-weight: bold;">var</span> timeStampComment <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&lt;!-- &quot;</span> <span style="color: #339933;">+</span> day <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'s menu last cached: &quot;</span> <span style="color: #339933;">+</span> cacheTimeStamp <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; --&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	  <span style="color: #000066; font-weight: bold;">return</span> timeStampComment<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There&#8217;s one big gotcha in the code above.  Notice how I&#8217;m setting the descr variable in both the buildMenuRow() and the buildCategoryRow() functions.  If you don&#8217;t use array notation with @attributes (['@attributes']), this code will fail in IE.</p>
<p>Finally, let&#8217;s take a look at how I displayed the menu on the cafeteria home page and on the daily menu page.</p>
<p>This first snippet displays the sidebar menu on the cafeteria homepage.  Since the homepage always displays the current day&#8217;s menu, the only parameter that I&#8217;m passing with the querystring is the required callback.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// URL to menu display page</span>
    <span style="color: #003366; font-weight: bold;">var</span> menuLink <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://www.example.com/cafeteria/dailyMenu.shtml'</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</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>
&nbsp;
       <span style="color: #006600; font-style: italic;">// Get JSON representation of cafeteria menu for today</span>
        $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://www.cross-domain-example.com/service/cafeteria/json.php?callback=cafeteriaMenu&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">// Build html menu table</span>
            <span style="color: #003366; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> outputSidebarMenu<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">// Add table to .sidebarMenu div</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.sidebarMenu&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>content<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>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The last snippet displays the full sized menu on the daily menu page.  Since I only want a single daily menu page, rather than a page for each day of the week, I had to have some way to grab a &#8220;requestedDay&#8221; parameter from the querystring.  The jQuery <a title="Query String Object plugin" href="http://plugins.jquery.com/project/query-object">Query String Object plugin</a> solved that problem perfectly.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Menu display requires jQuery and jQuery querystring plugin (jQuery.query.js)</span>
    $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</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>
&nbsp;
          <span style="color: #006600; font-style: italic;">// JSON service URL</span>
        <span style="color: #003366; font-weight: bold;">var</span> jsonUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://www.cross-domain-example.com/service/cafeteria/json.php?callback=?'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Get requested day from QueryString</span>
        <span style="color: #006600; font-style: italic;">// @see http://plugins.jquery.com/project/query-object</span>
        <span style="color: #003366; font-weight: bold;">var</span> reqDay <span style="color: #339933;">=</span> $.<span style="color: #660066;">query</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'requestedDay'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        jsonUrl <span style="color: #339933;">=</span> jsonUrl <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&amp;day='</span> <span style="color: #339933;">+</span> reqDay.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Get JSON representation of cafeteria menu for today            </span>
        $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span>jsonUrl<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Add &quot;[day]'s Menu&quot; to page title</span>
            updatePageTitle<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Build html menu table</span>
            <span style="color: #003366; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> outputDailyMenu<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Add table to #menuBuilder div</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#menuBuilder&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>content<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>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Wrapping Up</strong></p>
<p>When you&#8217;re in a situation where you can&#8217;t use PHP (or your programming language of choice) to display dynamic content, AJAX can be an excellent solution.  With the right tools, good documentation, and a little time spent on Google, it&#8217;s relatively simple to overcome the limitations of your environment and deliver rich, up-to-date content to your users.  Combining Zend Framework, the jQuery JavaScript Library, and JSONP made for a nice, lightweight solution to the cafeteria menu problem, saving my clients and co-workers a lot of time and effort they can now expend elsewhere.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremykendall.net/2008/10/16/dynamic-content-on-static-pages-using-zend-json-and-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
