<?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>Diego Pessoa &#187; Jquery</title>
	<atom:link href="http://www.diegopessoa.com/lang/en/categories/web-developing/javascript/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.diegopessoa.com</link>
	<description></description>
	<lastBuildDate>Thu, 07 Jan 2010 04:22:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Solving the problem of &#8220;back&#8221; button in Ajax applications</title>
		<link>http://www.diegopessoa.com/lang/en/2009/03/09/solving-the-problem-of-back-button-in-ajax-applications</link>
		<comments>http://www.diegopessoa.com/lang/en/2009/03/09/solving-the-problem-of-back-button-in-ajax-applications#comments</comments>
		<pubDate>Tue, 10 Mar 2009 00:18:27 +0000</pubDate>
		<dc:creator>Diego Pessoa</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Jquery]]></category>
		<category><![CDATA[Web Standards]]></category>
		<category><![CDATA[Web developing]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Web Accessibility]]></category>

		<guid isPermaLink="false">http://www.diegopessoa.com/?p=102</guid>
		<description><![CDATA[














Who here has always dreamed in do Ajax applications that the user can freely navigate using the back and forward buttons, and furthermore, can bookmark specific sections? In theory, this isn’t possible, since the “Ajax” is used by most to reload just some page blocks, keeping active the main page and, consequently, precluding the back [...]]]></description>
			<content:encoded><![CDATA[














<p><span lang="en">Who here has always dreamed in do Ajax applications that the user can freely navigate using the back and forward buttons, and furthermore, can bookmark specific sections? In theory, this isn’t possible, since the “Ajax” is used by most to reload just some page blocks, keeping active the main page and, consequently, precluding the back button or bookmarks use. Otherwise we’d have the good old reload of the entire page.</span></p>
<p><span lang="en">But then, how the Gmail works? It’s totally in Ajax and yet you can navigate using the back button.  If you put a link like: http://mail.google.com/mail/#compose in the address bar, it will open the e-mail composition screen normally. The big secret is in the #anchors.<br />
Anchor Oriented Navigation</span></p>
<p><span lang="en">A very interesting thing of the anchors is that they allow you to use the back and forward button, for example, without generate no one full page request and without leaving that page, so, how could we do this?</span></p>
<p><span lang="en">The first step is create a Javascript file that will be every time verifying if some anchor is active, if so, it sends the Ajax request calling the specific section, based on anchor’s name, that will be loaded inside the main block. Simple, isn’t it?</span></p>
<p><span lang="en">As is usual for JavaScript today, we will use jQuery, so remember to include the library file in your application.</span></p>
<p><span lang="en">We would then have a file named &#8220;core.js&#8221;, thus:</span></p>
<div lang="en">
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">* Loading the page, the timer is started to check if there is an active anchor to each 300ms. */</span></div>
</li>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">ready</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">setInterval<span class="br0">&#40;</span><span class="st0">&#34;checkAnchor()&#34;</span>, <span class="nu0">300</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> currentAnchor = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">* Method to check if there is some anchor change, if so, sends the Ajax request.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">*/</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">function</span> checkAnchor<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span>currentAnchor != document.<span class="me1">location</span>.<span class="me1">hash</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">currentAnchor = document.<span class="me1">location</span>.<span class="me1">hash</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">* If doesn’t have no one active anchor, the default section is loaded</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">*/</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span>!currentAnchor<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">query = <span class="st0">&#34;section=home&#34;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">* Creates the result string. Converting the url: URL/#fotos&#38;amp;id=2 in URL/?section=fotos&#38;amp;id=2</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">* */</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> splits = currentAnchor.<span class="me1">substring</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&#8216;&#38;amp;&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> section = splits<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">delete</span> splits<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> params = splits.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&#8216;&#38;amp;&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> query = <span class="st0">&#34;section=&#34;</span> + section + params;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">* Sends the request</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">*/</span></div>
</li>
<li class="li1">
<div class="de1">$.<span class="me1">get</span><span class="br0">&#40;</span><span class="st0">&#34;callbacks.php&#34;</span>,query, <span class="kw2">function</span><span class="br0">&#40;</span>data<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span><span class="st0">&#34;#main_block&#34;</span><span class="br0">&#41;</span>.<span class="me1">html</span><span class="br0">&#40;</span>data<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
</div>
<p><span lang="en">OK, the content will be loaded in th e element of ID “main_block” through the Ajax request result that being sent to the page “callbacks.php” in GET Querystring format.</span></p>
<p><span lang="en">To catch it, we would have something like this:</span></p>
<div lang="en">
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$section</span> = <a href="http://www.php.net/addslashes"><span class="kw3">addslashes</span></a><span class="br0">&#40;</span><a href="http://www.php.net/strip_tags"><span class="kw3">strip_tags</span></a><span class="br0">&#40;</span><span class="re0">$_GET</span><span class="br0">&#91;</span><span class="st0">&#8217;section&#8217;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$page</span> = <span class="st0">&#34;pages/&#34;</span>.<span class="re0">$section</span>.<span class="st0">&#34;.php&#34;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/file_exists"><span class="kw3">file_exists</span></a><span class="br0">&#40;</span><span class="re0">$page</span><span class="br0">&#41;</span><span class="br0">&#41;</span>  <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">include</span><span class="br0">&#40;</span><span class="re0">$page</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
</div>
<p><span lang="en">Thus, we would need only a folder called “pages” containing the pages with the anchors’ names.</span></p>
<p><span lang="en">And to call each section, a simple html page with links like:</span></p>
<p><span lang="en">http://mysite.com/#photos</span></p>
<p><span lang="en">It would be detected by the Javascript method “checkAnchor()”, and will be call a php request loading the page named photos.php inside the directory “pages”. It will exhibited inside the block “#main_block” that is contained in the same page of the clicked link, and the best of all, you can back, forward and bookmark the sections you want.</span></p>
<p><span lang="en">To better illustrate the example, I made a blatant copy (haha) of the PHP-PB’s site, with the only difference of having Ajax navigation through anchors (the original has normal navigation). Access the link: <a href="http://www.diegopessoa.com/php-pb-ajax" target="_blank">http://www.diegopessoa.com/php-pb-ajax</a></span></p>
<p><span lang="en">If you want to download the working example, <a href="http://diegopessoa.com/php-pb-ajax/%5bdiegopessoa.com%5dAjaxBack.zip" target="_blank">click here</a>!﻿</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.diegopessoa.com/lang/en/2009/03/09/solving-the-problem-of-back-button-in-ajax-applications/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
