<?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>SciBuff.com &#187; unobtrusive</title>
	<atom:link href="http://www.scibuff.com/tag/unobtrusive/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scibuff.com</link>
	<description>Science Blog</description>
	<lastBuildDate>Tue, 31 Aug 2010 21:21:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Unobtrusive Ajax Navigation &#8211; Version 3</title>
		<link>http://www.scibuff.com/2009/06/15/unobtrusive-ajax-navigation-version-3/</link>
		<comments>http://www.scibuff.com/2009/06/15/unobtrusive-ajax-navigation-version-3/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 13:24:08 +0000</pubDate>
		<dc:creator>scibuff</dc:creator>
				<category><![CDATA[Science]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[unobtrusive]]></category>

		<guid isPermaLink="false">http://www.scibuff.com/?p=538</guid>
		<description><![CDATA[Last week we continued the tutorial on Unobtrusive AJAX Navigation by cleaning up our Javascript code as well as adding functionality which enabled users to bookmark individual pages and use the &#8220;Back&#8221; and &#8220;Forward&#8221; browser buttons for navigation. This time, we&#8217;ll focus on cleaning up the back-end scripts to make the solution easily maintainable and [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Unobtrusive Ajax Navigation - Version 2" href="http://www.scibuff.com/2009/06/08/unobtrusive-ajax-navigation-version-2/" target="_blank">Last week</a> we continued the tutorial on Unobtrusive AJAX Navigation by cleaning up our Javascript code as well as adding functionality which enabled users to bookmark individual pages and use the &#8220;Back&#8221; and &#8220;Forward&#8221; browser buttons for navigation. This time, we&#8217;ll focus on cleaning up the back-end scripts to make the solution easily maintainable and expandable. Check out the <a rel="nofollow" href="http://www.scibuff.com/dev/ajax/ajax-version-3.0/" target="_blank">fully functional demo</a> and/or <a rel="nofollow" href="http://www.scibuff.com/dev/ajax/ajax-version-3.0/ajax-version-3.0.zip" target="_blank">download the files for this tutorial</a>.</p>
<p>There are two equally good approaches to achieve our goal. One is using simple server-side include (SSI) instructions to keep common content in separate files and include them into our pages at the time when it is needed. The second approach is via Object Oriented (OO) PHP. I am a big fan of OO but too often I see it misused by self-pronounced programmers who naively think that throwing OO concepts on everything makes them better coders (nothing could be further from the truth). There is place and time for deciding to &#8220;complicate&#8221; your file structure but unless you&#8217;re about to create a solution of the size of WordPress (for example) you should think really hard about the pro&#8217;s and con&#8217;s. Unless your intention is to build a robust, easily extendable and maintainable product with support for hundreds of thousands of users, good old procedural scripting will do the job just as well (if not better).</p>
<p>So without much further ado, let us look at the back-end code from the previous projects:</p>
<p>The first thing that is immediately obvious is that the majority of code in &#8220;index.php&#8221; and &#8220;about.php&#8221; is identical. Let&#8217;s split this code into appropriate files. Create a directory called &#8220;inc&#8221; and place the following &#8220;.htaccess&#8221; file in it:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Order Allow<span style="color: #339933;">,</span>Deny
Deny From All</pre></div></div>

<p>All this file does is it prevents any sort of (GET, POST, HEAD etc) requests made directly to the /inc directory. Next, we create a file called &#8220;header.php&#8221; and place the &#8220;print_header&#8221; function in it:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
&nbsp;
<span style="color: #009900;">function print_header<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">?&gt;</span>
&nbsp;
	...
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;version-1-0&quot;</span>&gt;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!-- WRAPPER - START --&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;wrapper&quot;</span>&gt;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">&lt;!-- HEADER - START --&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;header&quot;</span>&gt;</span>
&nbsp;
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Unobtrusive Ajax Navigation - Version 3.0<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
&nbsp;
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> &gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.php&quot;</span>&gt;</span>Home<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;about.php&quot;</span>&gt;</span>About<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
&nbsp;
 			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
 		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
 		<span style="color: #808080; font-style: italic;">&lt;!-- HEADER - END --&gt;</span>
&nbsp;
 		<span style="color: #808080; font-style: italic;">&lt;!-- CONTENT - START --&gt;</span>
 		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;content&quot;</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span>
<span style="color: #009900;">?&gt;</span></pre></div></div>

<p>Similarly, we create &#8220;footer.php&#8221; and place the &#8220;print_footer&#8221; function code in it.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"> <span style="color: #009900;">&lt;?php</span>
&nbsp;
<span style="color: #009900;">function print_footer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">?&gt;</span>
 	 	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>            
 		 <span style="color: #808080; font-style: italic;">&lt;!-- CONTENT - END --&gt;</span>
&nbsp;
 		 <span style="color: #808080; font-style: italic;">&lt;!-- FOOTER - START --&gt;</span>
 		 <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;footer&quot;</span>&gt;</span>
 	 		 <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Footer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
 	 	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
 	 	<span style="color: #808080; font-style: italic;">&lt;!-- FOOTER - END --&gt;</span>
 	 	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span>
<span style="color: #009900;">?&gt;</span></pre></div></div>

<p>Then create another two include files, one for the content of &#8220;index.php&#8221; and one for the content of &#8220;about.php&#8221;. Let&#8217;s name them &#8220;index.inc.php&#8221; and &#8220;about.inc.php&#8221; respectively, and place the &#8220;print_content&#8221; functions in them, like so</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
&nbsp;
<span style="color: #009900;">function print_content<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">?&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Homepage<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Lorem ipsum ...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span>
<span style="color: #009900;">?&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
&nbsp;
<span style="color: #009900;">function print_content<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">?&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>About<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Lorem ipsum ...<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
<span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #009900;">?&gt;</span></pre></div></div>

<p>Next, create &#8220;content.php&#8221; and place the code from above the &#8220;print_header&#8221; function in &#8220;index.php&#8221; (or about.php) into a newly created &#8220;print_page&#8221; function, like so</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> print_page<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$page</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inc/header.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inc/footer.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$page</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// if the page is requested by AJAX</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;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ajax'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ajax'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;true&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		print_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		print_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		print_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		print_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Perhaps, this new function requires a bit of explaining: We want to remove all (as much as possible) code duplicity, so create a function which will print a page content, just as we did before (depending on whether the request came directly from a browser or was requesting by our AJAX Javascript code). The only difference is that we use the &#8220;$page&#8221; argument to tell the function which page needs to be printed. The script will use the &#8220;print_content&#8221; function from the appropriate file.</p>
<p>Now, whenever you see something resembling</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$content_inc</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>your red alert flashing light should immediately go off. This is potentially a huge security risk: if someone could make the value of &#8220;$page&#8221; to be &#8220;.htaccess&#8221; or &#8220;.htpasswd&#8221; the user could read (encrypted) passwords and see other site settings. In the particular use of the construct, it is unlikely (nothing is impossible) that anyone would be able to exploit it. Nevertheless, it is possible that sometime in the future, to extend functionality or for some other purpose, the smallest modification to the code could leave us vulnerable against an attack. In short, the best practice is NEVER to trust the user, especially when a potential security flaw can easily be fixed by restricting the possible value set. Add the following lines before the function declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PAGE_HOME&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PAGE_ABOUT&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PAGE_HOME_SSI&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;home.inc.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PAGE_ABOUT_SSI&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;about.inc.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>change the function declaration to</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> print_page<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> PAGE_HOME <span style="color: #009900;">&#41;</span></pre></div></div>

<p>and add the following switch statement:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$page</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> PAGE_ABOUT <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span> PAGE_ABOUT_SSI <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">case</span> PAGE_HOME <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// fall through to default</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">default</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span> PAGE_HOME_SSI <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The few extra lines of code is all that is necessary to prevent the possibility of a large headache (at best) in the future.</p>
<p>Now, all that&#8217;s left is to use the newly created files and functions in our original scripts. Thus, we go to &#8220;index.php&#8221; and place the following lines of code in it:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inc/content.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
print_page<span style="color: #009900;">&#40;</span> PAGE_HOME <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Similarly, in &#8220;about.php&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inc/content.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
print_page<span style="color: #009900;">&#40;</span> PAGE_ABOUT <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>We will conclude this tutorial by adding a page called &#8220;Contact&#8221;. First of all, create a &#8220;contact.php&#8221; in the main folder and place the following code inside it:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inc/content.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
print_page<span style="color: #009900;">&#40;</span> PAGE_CONTACT <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Then open the &#8220;content.php&#8221; inside the &#8220;inc&#8221; folder and add</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PAGE_CONTACT&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PAGE_CONTACT_SSI&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;contact.inc.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>as well as</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">		<span style="color: #b1b100;">case</span> PAGE_CONTACT<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span> PAGE_CONTACT_SSI <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span></pre></div></div>

<p>into the switch statement. Then, let&#8217;s add the Contact Page to our navigation by placing the following into the &#8220;header.php&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax-link&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;contact.php&quot;</span>&gt;</span>Contact<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span></pre></div></div>

<p>Finally, we need to create a &#8220;contact.inc.php&#8221; inside the &#8220;inc&#8221; directory with a &#8220;print_content&#8221; function:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;?php</span>
&nbsp;
<span style="color: #009900;">function print_content<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">?&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Contact<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
&nbsp;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Address Line 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Address Line 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>City, Zip Code<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>Country<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;?php</span>
<span style="color: #009900;"><span style="color: #66cc66;">&#125;</span></span>
<span style="color: #009900;">?&gt;</span></pre></div></div>

<p>Next time, we will look at creating Unobtrusive AJAX Navigation as a part of a complex product (using OO) as well as making URL more user friendly (although search engine are perfectly capable of indexing links with query parameters there is an elegant way to make links more user friendly).</p>
<p>Meanwhile, check out a <a rel="nofollow" href="http://www.scibuff.com/dev/ajax/ajax-version-3.0/" target="_blank">fully functional demo of version 3</a> and/or <a rel="nofollow" href="http://www.scibuff.com/dev/ajax/ajax-version-3.0/ajax-version-3.0.zip" target="_blank">download the files for this tutorial</a>.</p>

<!-- WordPress Connect Modules v1.05 -->]]></content:encoded>
			<wfw:commentRss>http://www.scibuff.com/2009/06/15/unobtrusive-ajax-navigation-version-3/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Unobtrusive Ajax Navigation &#8211; Version 2</title>
		<link>http://www.scibuff.com/2009/06/08/unobtrusive-ajax-navigation-version-2/</link>
		<comments>http://www.scibuff.com/2009/06/08/unobtrusive-ajax-navigation-version-2/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 09:28:51 +0000</pubDate>
		<dc:creator>scibuff</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[unobtrusive]]></category>

		<guid isPermaLink="false">http://www.scibuff.com/?p=412</guid>
		<description><![CDATA[Last week I started a new series about creating unobtrusive Ajax navigation. The first version represented a good start and laid a solid foundation. Nevertheless, it was far from an ideal solution mainly for the lack of some basic functionality and poor organization of both the front- and back-end code. This tutorial will focus on [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Unobtrusive Ajax Navigation - Version 1" href="http://www.scibuff.com/2009/06/01/unobtrusive-ajax-navigation-version-1/" target="_blank">Last week</a> I started a new series about creating unobtrusive Ajax navigation. The first version represented a good start and laid a solid foundation. Nevertheless, it was far from an ideal solution mainly for the lack of some basic functionality and poor organization of both the front- and back-end code.</p>
<p>This tutorial will focus on cleaning up the Javascript code as well as adding functionality necessary to improve the user experience. Check out the <a href="http://www.scibuff.com/dev/ajax/ajax-version-2.0/" target="_blank">demo</a> and/or <a href="http://www.scibuff.com/dev/ajax/ajax-version-2.0/ajax-version-2.0.zip" target="_blank">download all files</a>.</p>
<p>In general, it is always a good practice to wrap all code within a unique namespace to prevent code collisions. The following few lines of code accomplish that:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>com <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #003366; font-weight: bold;">var</span> com <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>com.<span style="color: #660066;">scibuff</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> com.<span style="color: #660066;">scibuff</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>com.<span style="color: #660066;">scibuff</span>.<span style="color: #660066;">dev</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> com.<span style="color: #660066;">scibuff</span>.<span style="color: #660066;">dev</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>com.<span style="color: #660066;">scibuff</span>.<span style="color: #660066;">dev</span>.<span style="color: #660066;">ajax</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	com.<span style="color: #660066;">scibuff</span>.<span style="color: #660066;">dev</span>.<span style="color: #660066;">ajax</span> <span style="color: #339933;">=</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: #006600; font-style: italic;">// private fields</span>
		<span style="color: #003366; font-weight: bold;">var</span> tmp <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// public fields</span>
		<span style="color: #003366; font-weight: bold;">var</span> pub <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> pub<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There are two objects inside the newly created object (com.scibuff.dev.ajax), i.e. <em>tm</em>p and <em>pub</em>. The former will be used for internal methods, variables and constants for which there is no need to have public access, whereas the later will be accessible via the <em>window</em> object, i.e <em>com.scibuff.dev.ajax</em>.</p>
<p>Now let&#8217;s take the code from the version 1 Javascpript file and place it into the newly created structure</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">		tmp.<span style="color: #660066;">setAjaxLink</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> element<span style="color: #339933;">,</span> clickEvent <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			clickEvent.<span style="color: #660066;">preventDefault</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;">var</span> url <span style="color: #339933;">=</span> element.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			tmp.<span style="color: #660066;">loadPageContent</span><span style="color: #009900;">&#40;</span> url<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;#content&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;
		tmp.<span style="color: #660066;">loadPageContent</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> url<span style="color: #339933;">,</span> destination <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			data<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span> destination <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span> url<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'ajax'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'true'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</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> destination <span style="color: #339933;">+</span> <span style="color: #3366CC;">' a.ajax-link'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					tmp.<span style="color: #660066;">setAjaxLink</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> event <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>
		<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Since the <em>tmp</em> object is used to encapsulate internal functions we will need a point of entry from outside of the namespace. We use the <em>pub</em> object and adding the following few lines of code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">		pub.<span style="color: #660066;">init</span> <span style="color: #339933;">=</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;">'a.ajax-link'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				tmp.<span style="color: #660066;">setAjaxLink</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> event <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></pre></div></div>

<p>Now we simply call the newly created function once the DOM object is fully loaded like so:</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>
	com.<span style="color: #660066;">scibuff</span>.<span style="color: #660066;">dev</span>.<span style="color: #660066;">ajax</span>.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</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>From user experience perspective one of the most important features missing in the first version was the ability to bookmark a specific page (and share it on social network sites). This functionality can be quite easily achieved using the URL hash to store necessary data.</p>
<p>We basically need two functions, one to parse the hash string into meaningful data object and one to take a data object and format it into a hash string.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	tmp.<span style="color: #660066;">HASH_VARS_SEPARATOR</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">';'</span>
	<span style="color: #006600; font-style: italic;">/**
	 * Parses the url hash string into a vars (JSON) object
	 */</span>
	tmp.<span style="color: #660066;">getHashVars</span> <span style="color: #339933;">=</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: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> url.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> hash <span style="color: #339933;">=</span> url.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> hashes <span style="color: #339933;">=</span> hash.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span> tmp.<span style="color: #660066;">HASH_VARS_SEPARATOR</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> hashes <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> hashes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #003366; font-weight: bold;">var</span> tuple <span style="color: #339933;">=</span> hashes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					data<span style="color: #009900;">&#91;</span> tuple<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> unescape<span style="color: #009900;">&#40;</span>tuple<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</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> data<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * Sets the url hash string using the values from the vars object
	 * @param {Object} vars
	 */</span>
	tmp.<span style="color: #660066;">setHashVars</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> vars <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> url.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> url <span style="color: #339933;">=</span> url.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;">var</span> hash <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #000066; font-weight: bold;">in</span> vars <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> vars<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				hash <span style="color: #339933;">+=</span> key <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> escape<span style="color: #009900;">&#40;</span>vars<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> tmp.<span style="color: #660066;">HASH_VARS_SEPARATOR</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		window.<span style="color: #660066;">location</span> <span style="color: #339933;">=</span> url <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;#&quot;</span> <span style="color: #339933;">+</span> hash<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we can incorporate these two new functions into the existing code. In the <em>tmp.loadPageContent</em> function, add</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> tmp.<span style="color: #660066;">getHashVars</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	data<span style="color: #009900;">&#91;</span> pub.<span style="color: #660066;">PAGE_VAR_NAME</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
	tmp.<span style="color: #660066;">setHashVars</span><span style="color: #009900;">&#40;</span> data <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>to change the URL whenever a new ajax request is made. Similarly, in the <em>pub.init</em> function we add</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> tmp.<span style="color: #660066;">getHashVars</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> data<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;page&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		tmp.<span style="color: #660066;">loadPageContent</span><span style="color: #009900;">&#40;</span> data<span style="color: #009900;">&#91;</span> pub.<span style="color: #660066;">PAGE_VAR_NAME</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;#content&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The load the page content from the page specified in the URL hash data when the user first arrives at our site. These few extra lines of code is all that is needed to give users the ability to deep-link and bookmark any page accessible from the Ajax Navigation.</p>
<p>Now we have functionality for updating the URL hash with every AJAX request and loading page content based on the data present in the hash. To finish off this piece of functionality, let&#8217;s enabled navigation using browser&#8217;s Back and Forward buttons as well as direct input to the browser&#8217;s address bar.</p>
<p>We need to create a process to listen to changes in the address bar URL. Unfortunately, browsers do not provide in-built events for the URL change, but we can easily mimic that functionality with a few lines of custom code using jQuery&#8217;s <em>trigger </em>and <em>bind </em>methods.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">	tmp.<span style="color: #660066;">URL_CHECK_INTERVAL</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
	tmp.<span style="color: #660066;">internalUrlChange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	tmp.<span style="color: #660066;">oldURL</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * Checks if the window url has changed and if so,
	 * triggers a &quot;change&quot; events
	 */</span>
	tmp.<span style="color: #660066;">checkURL</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> tmp.<span style="color: #660066;">oldURL</span> <span style="color: #339933;">!=</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			tmp.<span style="color: #660066;">oldURL</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</span><span style="color: #339933;">;</span>
			$<span style="color: #009900;">&#40;</span> window.<span style="color: #660066;">location</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trigger</span><span style="color: #009900;">&#40;</span>
				pub.<span style="color: #660066;">URL_CHANGED</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> url <span style="color: #339933;">:</span> window.<span style="color: #660066;">location</span>.<span style="color: #660066;">href</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;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">/**
	 * This function is executed every time the browser's url
	 * is changed
	 * @param {Object} event
	 * @param {Object} data
	 */</span>
	tmp.<span style="color: #660066;">urlChangeHandler</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event<span style="color: #339933;">,</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>tmp.<span style="color: #660066;">internalUrlChange</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> tmp.<span style="color: #660066;">getHashVars</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> data<span style="color: #009900;">&#91;</span> pub.<span style="color: #660066;">PAGE_VAR_NAME</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				tmp.<span style="color: #660066;">loadPageContent</span><span style="color: #009900;">&#40;</span> data<span style="color: #009900;">&#91;</span> pub.<span style="color: #660066;">PAGE_VAR_NAME</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;#content&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>				
&nbsp;
		<span style="color: #009900;">&#125;</span>
		tmp.<span style="color: #660066;">internalUrlChange</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	pub.<span style="color: #660066;">init</span> <span style="color: #339933;">=</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> window.<span style="color: #660066;">location</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span> pub.<span style="color: #660066;">URL_CHANGED</span><span style="color: #339933;">,</span> tmp.<span style="color: #660066;">urlChangeHandler</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setInterval<span style="color: #009900;">&#40;</span> tmp.<span style="color: #660066;">checkURL</span><span style="color: #339933;">,</span> tmp.<span style="color: #660066;">URL_CHECK_INTERVAL</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		...
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <em>tmp.checkURL</em> function will check the browser&#8217;s URL for changes every 0.1 second. Every time a change is detected, the <em>tmp.urlChangeHandler</em> function will validate the hash data and load the appropriate page content via <em>tmp.loadPageContent</em>. To prevent infinite loops (e.g. user clicks on a navigation link, which changes the hash triggering an AJAX request, which changes the hash, etc&#8230;) we need to add the tmp.internalUrlChange variable and set it to false whenever the URL change will be due to changing the hash internally (as opposed to input by the user).</p>
<p>Hopefully after these two tutorials, you can now create a fully functional unobtrusive AJAX navigation for your site(s). The next part of the series will tackle organization of the back-end code and error checking. Meanwhile, check out the <a href="http://www.scibuff.com/dev/ajax/ajax-version-2.0/" target="_blank">demo</a> of this tutorial and/or <a href="http://www.scibuff.com/dev/ajax/ajax-version-2.0/ajax-version-2.0.zip" target="_blank">download the files</a> used in this tutorial.</p>

<!-- WordPress Connect Modules v1.05 -->]]></content:encoded>
			<wfw:commentRss>http://www.scibuff.com/2009/06/08/unobtrusive-ajax-navigation-version-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unobtrusive Ajax Navigation &#8211; Version 1</title>
		<link>http://www.scibuff.com/2009/06/01/unobtrusive-ajax-navigation-version-1/</link>
		<comments>http://www.scibuff.com/2009/06/01/unobtrusive-ajax-navigation-version-1/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 11:12:27 +0000</pubDate>
		<dc:creator>scibuff</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[unobtrusive]]></category>

		<guid isPermaLink="false">http://www.scibuff.com/?p=406</guid>
		<description><![CDATA[This is the first post in my new series about creating unobtrusive AJAX navigation for a website using Javascript frameworks. I still remember the first time I saw AJAX in action on the Gmail registration page a while back. It gave users the option to check if a given username was taken, and it did [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first post in my new series about creating unobtrusive AJAX navigation for a website using Javascript frameworks. I still remember the first time I saw AJAX in action on the Gmail registration page a while back. It gave users the option to check if a given username was taken, and it did the check asynchronously, i.e. the HTML page did not have to be reloaded.</p>
<p>AJAX navigation is not only cool, it is also fast and saves bandwidth. It can be seen around the web (Gmail or Facebook for example) but many pages are missing a fall-back for users without Javascript, or on mobile devices. Javascript should be used ONLY to enhance the usability of web sites and NEVER to create functionality that does not already exist. In this tutorial (the first of many) I&#8217;m going to demonstrate how easy it is to set up unobtrusive Ajax navigation using jQuery. </p>
<p>Check out the <a href="http://www.scibuff.com/dev/ajax/ajax-version-1.0/" target="_blank">demo</a> of this tutorial or <a href="http://www.scibuff.com/dev/ajax/ajax-version-1.0/ajax-version-1.0.zip" target="_blank">download all the files</a>.</p>
<p>First, create a simple web page.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;version-1&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;wrapper&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;header&quot;</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Unobtrusive Ajax Navigation - Version 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inline-list navigation-main&quot;</span> &gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.php&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax-link&quot;</span>&gt;</span>Home<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;about.php&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax-link&quot;</span>&gt;</span>About<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;clear-both&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;content&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;footer&quot;</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Footer<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span></pre></div></div>

<p>I took the liberty of adding some CSS styles to make the page look nice. The important part is adding <em>ajax-link</em> to the links&#8217; class name. In just a moment, it will be used it to add AJAX functionality to the navigation. As the first step include the jQuery libraries. I prefer to link to the Google code libraries as it not only saves the trouble of downloading/uploading jQuery for every project but the library is likely to already be in your browser&#8217;s cache (from other sites).</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>Next, add the project&#8217;s javascript file, I named it <em>ajax-version-1.0.js</em> and placed it into a <em>js</em> directory.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/ajax-version-1.0.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>That&#8217;s it for the HTML code. Now let&#8217;s have a look at the Javascript that actually does all the work for the AJAX navigation.</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;">'a.ajax-link'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		set_ajax_link<span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> event <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>
<span style="color: #003366; font-weight: bold;">function</span> set_ajax_link<span style="color: #009900;">&#40;</span> el<span style="color: #339933;">,</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	event.<span style="color: #660066;">preventDefault</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;">var</span> url <span style="color: #339933;">=</span> el.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	load_page_content<span style="color: #009900;">&#40;</span> url <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> load_page_content<span style="color: #009900;">&#40;</span> url <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#content&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span> url<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'ajax'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'true'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</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;">'#content a.ajax-link'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			set_ajax_link<span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> event <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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As a rule of thumb, I wrap my code within the <em>document ready</em> block, to ensure that DOM is fully loaded before trying to access its elements.</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;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The next part searches for all <em>a</em> tag elements which has been assigned a <em>className </em>of <em>ajax-link</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.ajax-link'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	set_ajax_link<span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> event <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>All such elements are then passed into my <em>set_ajax_link</em> function.  Also pass in the click event instance (we&#8217;ll see in a second why).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> set_ajax_link<span style="color: #009900;">&#40;</span> el<span style="color: #339933;">,</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	event.<span style="color: #660066;">preventDefault</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;">var</span> url <span style="color: #339933;">=</span> el.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	load_page_content<span style="color: #009900;">&#40;</span> url <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first command in the <em>set_ajax_link</em> function stops the default behavior when the user clicks on a link because the desired functionality is to load the content asynchronously instead of navigating to that page. Next, grab the link&#8217;s URL using the <em>attr</em> function, and as the last step  load the content from that URL using the <em>load_page_content</em> function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> load_page_content<span style="color: #009900;">&#40;</span> url <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#content&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span> url<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'ajax'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'true'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</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;">'#content a.ajax-link'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			set_ajax_link<span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> event <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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All that the <em>load_page_content</em> function has to accomplish is to load the content from the URL. This is achieved via jQuery&#8217;s <em>load</em> function. Once the content is loaded it will be placed into the div element with id &#8220;content&#8221;. Using the second argument of the <em>load</em> function the script will send a query parameter named <em>ajax</em> with value set to <em>true</em> along with the asynchronous request. The third argument points to a function that is executed once a response is received from the server. The script will give the AJAX navigation functionality to all links with <em>className</em> of <em>ajax-link</em> in the received response.</p>
<p>Using the HTML code created in the very first step and some new functionality create the homepage (name it index.php).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// if the page is requested by AJAX</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;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ajax'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ajax'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;true&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		print_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		print_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		print_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		print_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> print_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; dir=&quot;ltr&quot; lang=&quot;en-US&quot;&gt;
	&lt;head profile=&quot;http://gmpg.org/xfn/11&quot;&gt;
		&lt;title&gt;Unobtrusive Ajax Navigation - Version 1&lt;/title&gt;
&nbsp;
		&lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; /&gt;
&nbsp;
		&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;js/ajax-version-1.0.js&quot;&gt;&lt;/script&gt;
&nbsp;
	&lt;/head&gt;
	&lt;body id=&quot;version-1-0&quot;&gt;
		&lt;div id=&quot;wrapper&quot; class=&quot;border-module&quot;&gt;
			&lt;div id=&quot;header&quot; class=&quot;border-module&quot;&gt;
				&lt;h2&gt;Unobtrusive Ajax Navigation - Version 1.0&lt;/h2&gt;
				&lt;ul class=&quot;inline-list navigation-main&quot; &gt;
					&lt;li&gt;&lt;a href=&quot;index.php&quot; class=&quot;ajax-link&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
					&lt;li&gt;&lt;a href=&quot;about.php&quot; class=&quot;ajax-link&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
				&lt;/ul&gt;
				&lt;div class=&quot;clear-both&quot;&gt;&lt;/div&gt;
			&lt;/div&gt;
			&lt;div id=&quot;content&quot; class=&quot;border-module&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> print_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
			&lt;/div&gt;
			&lt;div id=&quot;footer&quot; class=&quot;border-module&quot;&gt;
				&lt;p&gt;Footer&lt;/p&gt;
			&lt;/div&gt;
		&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> print_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;h2&gt;Homepage&lt;/h2&gt;
	&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit...&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now HTML content of our index.php is identical to the static HTML page created originally. The difference is that the HTML code is split into three parts by three PHP functions, <em>print_header()</em>, <em>print_footer()</em> and <em>print_content()</em>. There are a few reasons for this separation. Some will be discussed/addressed in future tutorials. One of the reasons becomes clear when we examine the top of the index.php script page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// if the page is requested by AJAX</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;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ajax'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ajax'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;true&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	print_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	print_header<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	print_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	print_footer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we can see the reason behind added the <em>ajax</em> query parameter in our <em>load_page_content</em> Javascript function. The script will use it to determine whether the user arrived at the page directly (when the if statement evaluates to <em>false</em>) or the request came from the AJAX request. If the user arrives directly to our <em>index.php</em> page, the script will output the entire HTML code (using the <em>print_header()</em>, <em>print_footer()</em> and <em>print_content() </em>functions), however, the AJAX request will return the content ONLY (you can&#8217;t see that content by visiting <a href="http://www.scibuff.com/dev/ajax/ajax-version-1.0/index.php?ajax=true" target="_blank">index.php?ajax=true</a> directly).</p>
<p>As the final step, I created another page and name it <em>about.php</em>. Then I simply copied everything from out <em>index.php</em> over to <em>about.php</em> and change the content of the <em>print_content()</em> function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">function print_content(){
?&gt;
	&lt;h2&gt;About&lt;/h2&gt;
	&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit...&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The first version on a simple website using unobtrusive Ajax navigation is now finished. It has only to pages (<em>Home </em>and <em>About</em>) but can easily be extended to as many pages as necessary. The main advantage is that if user&#8217;s browser does not have Javascript support (or Javascript is disabled), the user will be able to navigate our sites in the conventional manner by simply clicking on the navigation links. Furthermore, the AJAX functionality can be developed independently of other aspects of the sites, i.e. we can have different people working simultaneously on the front-end and back-end.</p>
<p>Nevertheless, this version is quite far from an optimal solution and has many drawbacks:</p>
<ul>
<li>it does not allow users to bookmark the page they are currently viewing</li>
<li>there is no support for the browsers Back and Forward buttons</li>
<li>it restricts placing the content from asynchronous calls only to the div element with id &#8220;content&#8221;</li>
<li>the PHP code has as many duplicates are there are pages &#8211; <em>print_header()</em> and <em>print_footer()</em> are identical across all pages and should be in a single file</li>
<li>the Javascript code does not prevent possible conflicts with other scripts which could use functions with same names</li>
<li>the are no error checks or error messages</li>
<li>no indication of content being loaded</li>
<li>etc&#8230;</li>
</ul>
<p>I will address all these issues in future tutorials on this subject. Meanwhile, check out the <a href="http://www.scibuff.com/dev/ajax/ajax-version-1.0/" target="_blank">demo</a> of this tutorial or <a href="http://www.scibuff.com/dev/ajax/ajax-version-1.0/ajax-version-1.0.zip" target="_blank">download all the files</a>.</p>

<!-- WordPress Connect Modules v1.05 -->]]></content:encoded>
			<wfw:commentRss>http://www.scibuff.com/2009/06/01/unobtrusive-ajax-navigation-version-1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
