<?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>Richard Knop&#039;s Zend Framework Blog &#187; jQuery</title>
	<atom:link href="http://blog.richardknop.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.richardknop.com</link>
	<description>Zend Framework, PHP, MySQL, jQuery, JavaScript, AJAX, SEO, E-commerce and more</description>
	<lastBuildDate>Mon, 06 Sep 2010 15:49:00 +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>JQuery autosave on click event</title>
		<link>http://blog.richardknop.com/2009/09/jquery-autosave-on-click-event/</link>
		<comments>http://blog.richardknop.com/2009/09/jquery-autosave-on-click-event/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 19:54:28 +0000</pubDate>
		<dc:creator>Richard Knop</dc:creator>
				<category><![CDATA[JavaScript and AJAX]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.richardknop.com/?p=460</guid>
		<description><![CDATA[What does this simple jQuery snippet do? When you click on the link with class &#8220;autosave&#8221;, the form with id &#8220;autosave&#8221; is submitted via POST request to the same page. After that it redirects to the actual target of the link (href attribute). $&#40;document&#41;.ready&#40;function&#40;&#41; &#123; &#160; &#160; $&#40;&#39;a.autosave&#39;&#41;.click&#40;function&#40;&#41; &#123; &#160; &#160; &#160; &#160; // get [...]]]></description>
			<content:encoded><![CDATA[<p>What does this simple jQuery snippet do? When you click on the link with class &#8220;autosave&#8221;, the form with id &#8220;autosave&#8221; is submitted via POST request to the same page. After that it redirects to the actual target of the link (href attribute).</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span>document<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">&nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&#39;a.autosave&#39;</span><span class="br0">&#41;</span>.<span class="me1">click</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">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// get relative address of the current window/tab</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> url = window.<span class="me1">location</span>.<span class="me1">pathname</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// collect the data from the form with id &quot;autosave-form&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; $postData = $<span class="br0">&#40;</span><span class="st0">&#39;form#autosave&#39;</span><span class="br0">&#41;</span>.<span class="me1">serialize</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// submit the form</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; $.<span class="me1">post</span><span class="br0">&#40;</span>url, $postData, <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">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// after the ajax request is successful, redirect to the link target</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.<span class="me1">location</span>.<span class="me1">href</span> = $<span class="br0">&#40;</span><span class="st0">&#39;#autosave&#39;</span><span class="br0">&#41;</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="st0">&#39;href&#39;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// prevent default behavior</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span><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>
</ol>
</div>
<p>This can be useful in numerous situations. For example, on the page where user can edit his/her profile, you want to have a link saying something like &#8220;view my profile&#8221;. But you don&#8217;t want less experienced users to click on the link and lose all changes they have made to their profile (which happens quite a lot).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.richardknop.com/2009/09/jquery-autosave-on-click-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
