<?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>Using jQuery</title>
	<atom:link href="http://www.using-jquery.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.using-jquery.com</link>
	<description>About using Jquery</description>
	<lastBuildDate>Fri, 23 Dec 2011 14:02:59 +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>Proxy Pattern and overwriting functions in Javascript</title>
		<link>http://www.using-jquery.com/2011/11/proxy-pattern-and-overwriting-functions/</link>
		<comments>http://www.using-jquery.com/2011/11/proxy-pattern-and-overwriting-functions/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 20:15:50 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=307</guid>
		<description><![CDATA[I would like to present our readers a very interesting functionality that gives javascript developers a lot of power. First for those who don&#8217;t know what the Proxy Pattern is, well a proxy(at function level) is a function that provides a interface to another function. Let&#8217;s say we want to create a proxy for the ]]></description>
			<content:encoded><![CDATA[<p>I would like to present our readers a very interesting functionality that gives javascript developers a lot of power.</p>
<p>First for those who don&#8217;t know what the Proxy Pattern is, well a proxy(at function level) is a function that provides a interface to another function. </p>
<p>Let&#8217;s say we want to create a proxy for the console.log method. And have it do something else without altering it&#8217;s initial purpose.</p>
<p><span id="more-307"></span></p>
<p>Then by adding this code:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1"><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="kw2">var</span> proxied <span class="sy0">=</span> console<span class="sy0">.</span><span class="kw3">log</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; console<span class="sy0">.</span><span class="kw3">log</span> <span class="sy0">=</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;alert<span class="br0">&#40;</span>arguments<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">return</span> proxied<span class="sy0">.</span>apply<span class="br0">&#40;</span>this<span class="sy0">,</span> arguments<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>We will change the console.log function to also make an alert with the parameter it receives.<br />
How do we do this?&#8230;Well we first wrap everything in a closure so we can have access to the &#8216;proxied&#8217; variable. Then we save the console.log function in the closure and then we overwrite it. The proxy then alerts all data received when calling console.log and then delegates the call to the original function. Using apply(this, arguments) guarantees that the caller won&#8217;t be able to notice the difference between the original and the proxied method.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2011/11/proxy-pattern-and-overwriting-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery 1.7 beta released</title>
		<link>http://www.using-jquery.com/2011/10/jquery-1-7-beta-released/</link>
		<comments>http://www.using-jquery.com/2011/10/jquery-1-7-beta-released/#comments</comments>
		<pubDate>Sat, 01 Oct 2011 12:53:04 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=304</guid>
		<description><![CDATA[The jQuery 1.7 release is very expected so I&#8217;ve heared a beta of the release is out there. THe jQuery 1.7 will bring some new events API: on/off, some form behaviour changes to deal with IE 6/7/8, and some improvements to the animation function and the removeData function. Check out more on the jQuery page]]></description>
			<content:encoded><![CDATA[<p>The jQuery 1.7 release is very expected so I&#8217;ve heared a beta of the release is out there.</p>
<p>THe jQuery 1.7 will bring some new events API: on/off, some form behaviour changes to deal with IE 6/7/8, and some improvements to the animation function and the removeData function.</p>
<p>Check out more on the jQuery page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2011/10/jquery-1-7-beta-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery event namespacing.</title>
		<link>http://www.using-jquery.com/2011/08/jquery-event-namespacing/</link>
		<comments>http://www.using-jquery.com/2011/08/jquery-event-namespacing/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 08:09:32 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=300</guid>
		<description><![CDATA[One day I had a very interesting problem. I created a plugin with a scroll event that I had to add/remove, but I realized I can add the event but then how do I remove just my event? .. a .unbind(&#8216;scroll&#8217;) call would remove all scroll events, not only the event added by me. So ]]></description>
			<content:encoded><![CDATA[<p>One day I had a very interesting problem. I created a plugin with a scroll event that I had to add/remove, but I realized I can add the event but then how do I remove just my event? .. a .unbind(&#8216;scroll&#8217;) call would remove all scroll events, not only the event added by me.</p>
<p>So after a little digging I found &#8220;Namespaced events&#8221; support in jQuery. What does this mean?&#8230;Well one can add a event within a specific namespace and then remove that events from that namespace.</p>
<p>Here is a example, in which I will add a scoll event then remove my scoll event:<br />
<span id="more-300"></span></p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1"><span class="co1">//add event</span></div>
</li>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span>window<span class="br0">&#41;</span><span class="sy0">.</span>bind<span class="br0">&#40;</span><span class="st0">&#39;scroll.customNamespace&#39;</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//remove event</span></div>
</li>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span>window<span class="br0">&#41;</span><span class="sy0">.</span>unbind<span class="br0">&#40;</span><span class="st0">&#39;scroll.customNamespace&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Then event namespacing is very usefull in any plugin development and is a good to know functionality for day to day development.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2011/08/jquery-event-namespacing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Mobile Development (jQueryMobile)</title>
		<link>http://www.using-jquery.com/2011/08/jquery-mobile-development-jquerymobile/</link>
		<comments>http://www.using-jquery.com/2011/08/jquery-mobile-development-jquerymobile/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 12:32:21 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=291</guid>
		<description><![CDATA[For those interested in mobile app development I would like to present a solution for creating web apps for mobile devices using the jQuery mobile framework. The jQuery mobile framework helps create web apps using the simple &#8220;write less, do more&#8221; on which the jQuery framework was build. Using the framework creating smart and touch-optimized ]]></description>
			<content:encoded><![CDATA[<p>For those interested in mobile app development I would like to present a solution for creating web apps for mobile devices using the jQuery mobile framework.</p>
<p>The jQuery mobile framework helps create web apps using the simple &#8220;write less, do more&#8221; on which the jQuery framework was build. Using the framework creating smart and touch-optimized web apps is very easy.</p>
<p>The framework allows for creating apps that work across mobile platforms like iOS, Android or any other.</p>
<p>This is the blueprint for a mobile page:<br />
<span id="more-291"></span></p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1"><span class="sy0">&lt;</span>div data<span class="sy0">-</span>role<span class="sy0">=</span><span class="st0">&quot;page&quot;</span><span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sy0">&lt;</span>div data<span class="sy0">-</span>role<span class="sy0">=</span><span class="st0">&quot;header&quot;</span><span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sy0">&lt;</span>h1<span class="sy0">&gt;</span>Page Title<span class="sy0">&lt;/</span>h1<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sy0">&lt;/</span>div<span class="sy0">&gt;&lt;!&#8211;</span> <span class="sy0">/</span><span class="kw3">header</span> <span class="sy0">&#8211;&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sy0">&lt;</span>div data<span class="sy0">-</span>role<span class="sy0">=</span><span class="st0">&quot;content&quot;</span><span class="sy0">&gt;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sy0">&lt;</span>p<span class="sy0">&gt;</span>Page content goes here<span class="sy0">.&lt;/</span>p<span class="sy0">&gt;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sy0">&lt;/</span>div<span class="sy0">&gt;&lt;!&#8211;</span> <span class="sy0">/</span>content <span class="sy0">&#8211;&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sy0">&lt;</span>div data<span class="sy0">-</span>role<span class="sy0">=</span><span class="st0">&quot;footer&quot;</span><span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="sy0">&lt;</span>h4<span class="sy0">&gt;</span>Page Footer<span class="sy0">&lt;/</span>h4<span class="sy0">&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="sy0">&lt;/</span>div<span class="sy0">&gt;&lt;!&#8211;</span> <span class="sy0">/</span>footer <span class="sy0">&#8211;&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&lt;/</span>div<span class="sy0">&gt;&lt;!&#8211;</span> <span class="sy0">/</span>page <span class="sy0">&#8211;&gt;</span></div>
</li>
</ol>
</div>
<p>Using the jQuery mobile framework, we can easily create dialogs, toolbars, buttons, forms, lists and offers support for events like tap, taphold, swipe, orientationchange and many other functionalities.</p>
<p>More information can be found on http://jquerymobile.com/.</p>
<div id="attachment_292" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.using-jquery.com/wp-content/uploads/2011/08/jquery-mobile.jpg"><img class="size-medium wp-image-292" title="jquery-mobile" src="http://www.using-jquery.com/wp-content/uploads/2011/08/jquery-mobile-300x155.jpg" alt="jQuery mobile framework" width="300" height="155" /></a><p class="wp-caption-text">jQuery mobile framework</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2011/08/jquery-mobile-development-jquerymobile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery File Upload with Drag&amp;Drop</title>
		<link>http://www.using-jquery.com/2011/07/jquery-file-upload-with-dragdrop/</link>
		<comments>http://www.using-jquery.com/2011/07/jquery-file-upload-with-dragdrop/#comments</comments>
		<pubDate>Thu, 14 Jul 2011 08:26:12 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=284</guid>
		<description><![CDATA[In today&#8217;s article I would like to write about a very interesting functionality that can be developed using the latest html5 javascript. In this article I would like to explain how to make a simple drag&#038;drop script for uploading files, using the dragover and drop events that have been added with the new HTML5 javascript ]]></description>
			<content:encoded><![CDATA[<p>In today&#8217;s article I would like to write about a very interesting functionality that can be developed using the latest html5 javascript.<br />
In this article I would like to explain how to make a simple drag&#038;drop script for uploading files, using the dragover and drop events that have been added with the new HTML5 javascript update. So again, this functionality will work only on HTML5 compatible web-browsers.</p>
<p>Ok, so let&#8217; start creating our simple file upload functionality, first we will add the drag events:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1"><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;$<span class="br0">&#40;</span>document<span class="br0">&#41;</span><span class="sy0">.</span>ready<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; $<span class="br0">&#40;</span><span class="st0">&#39;#dragzone&#39;</span><span class="br0">&#41;</span><span class="sy0">.</span>bind<span class="br0">&#40;</span><span class="st0">&#39;dragover&#39;</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<span class="br0">&#40;</span>event<span class="sy0">.</span>target<span class="br0">&#41;</span><span class="sy0">.</span>css<span class="br0">&#40;</span><span class="st0">&#39;background-color&#39;</span><span class="sy0">,</span><span class="st0">&#39;red&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;event<span class="sy0">.</span>stopPropagation<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;event<span class="sy0">.</span>preventDefault<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; $<span class="br0">&#40;</span><span class="st0">&#39;#dragzone&#39;</span><span class="br0">&#41;</span><span class="sy0">.</span>bind<span class="br0">&#40;</span><span class="st0">&#39;drop&#39;</span><span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>event<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>event<span class="sy0">.</span>target<span class="br0">&#41;</span><span class="sy0">.</span>css<span class="br0">&#40;</span><span class="st0">&#39;background-color&#39;</span><span class="sy0">,</span><span class="st0">&#39;white&#39;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;$<span class="sy0">.</span>FileUpload<span class="br0">&#40;</span>$<span class="br0">&#40;</span>event<span class="sy0">.</span>target<span class="br0">&#41;</span><span class="sy0">.</span>attr<span class="br0">&#40;</span><span class="st0">&#39;path&#39;</span><span class="br0">&#41;</span><span class="sy0">,</span>event<span class="sy0">.</span>originalEvent<span class="sy0">.</span>dataTransfer<span class="sy0">.</span>files<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;event<span class="sy0">.</span>stopPropagation<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;event<span class="sy0">.</span>preventDefault<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span>jQuery<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p><span id="more-284"></span><br />
So what the events will do is that on &#8216;dragover&#8217;, so when we move the file over our &#8216;dragzone&#8217;, we set the color red, and when we drop the file we change the color back to white and send the files that have been dragged to our upload funtion. The upload function will use the new xhr2 object for uploading files using ajax requests.</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">$<span class="sy0">.</span>FileUpload <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>path<span class="sy0">,</span>files<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> x <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> x <span class="sy0">&lt;</span> files<span class="sy0">.</span>length<span class="sy0">;</span> x<span class="sy0">++</span><span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="kw3">file</span> <span class="sy0">=</span> files<span class="sy0">.</span>item<span class="br0">&#40;</span>x<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw2">var</span> xhr <span class="sy0">=</span> <span class="kw2">new</span> XMLHttpRequest<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; fileUpload <span class="sy0">=</span> xhr<span class="sy0">.</span>upload<span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; xhr<span class="sy0">.</span>onreadystatechange <span class="sy0">=</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="kw1">if</span> <span class="br0">&#40;</span>xhr<span class="sy0">.</span>readyState <span class="sy0">==</span> <span class="nu0">4</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;response <span class="sy0">=</span> $<span class="sy0">.</span>parseJson<span class="br0">&#40;</span>xhr<span class="sy0">.</span>responseText<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert<span class="br0">&#40;</span>response<span class="sy0">.</span>status<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;fileUpload<span class="sy0">.</span>onprogress <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>e<span class="sy0">.</span>lengthComputable<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> _progress <span class="sy0">=</span> Math<span class="sy0">.</span><span class="kw3">round</span><span class="br0">&#40;</span><span class="br0">&#40;</span>e<span class="sy0">.</span>loaded <span class="sy0">*</span> <span class="nu0">100</span><span class="br0">&#41;</span> <span class="sy0">/</span> e<span class="sy0">.</span>total<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>_progress <span class="sy0">!=</span> <span class="nu0">100</span><span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&#39;#upload_progress_&#39;</span><span class="sy0">+</span>x<span class="br0">&#41;</span><span class="sy0">.</span>text<span class="br0">&#40;</span>_progress <span class="sy0">+</span> <span class="st0">&#39;%&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;xhr<span class="sy0">.</span>open<span class="br0">&#40;</span><span class="st0">&quot;POST&quot;</span><span class="sy0">,</span> <span class="st0">&#39;index.php?path=&#39;</span><span class="sy0">+</span>encodeURIComponent<span class="br0">&#40;</span>path<span class="br0">&#41;</span><span class="sy0">+</span><span class="st0">&#39;&amp;name=&#39;</span><span class="sy0">+</span>encodeURIComponent<span class="br0">&#40;</span><span class="kw3">file</span><span class="sy0">.</span>fileName<span class="br0">&#41;</span><span class="sy0">,</span> <span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;xhr<span class="sy0">.</span>setRequestHeader<span class="br0">&#40;</span><span class="st0">&quot;Cache-Control&quot;</span><span class="sy0">,</span> <span class="st0">&quot;no-cache&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;xhr<span class="sy0">.</span>setRequestHeader<span class="br0">&#40;</span><span class="st0">&quot;X-Requested-With&quot;</span><span class="sy0">,</span> <span class="st0">&quot;XMLHttpRequest&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;xhr<span class="sy0">.</span>setRequestHeader<span class="br0">&#40;</span><span class="st0">&quot;X-File-Name&quot;</span><span class="sy0">,</span> encodeURIComponent<span class="br0">&#40;</span><span class="kw3">file</span><span class="sy0">.</span>fileName<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;xhr<span class="sy0">.</span>setRequestHeader<span class="br0">&#40;</span><span class="st0">&quot;X-File-Size&quot;</span><span class="sy0">,</span> <span class="kw3">file</span><span class="sy0">.</span><span class="kw3">fileSize</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;xhr<span class="sy0">.</span>setRequestHeader<span class="br0">&#40;</span><span class="st0">&quot;Content-Type&quot;</span><span class="sy0">,</span> <span class="st0">&quot;application/octet-stream&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;xhr<span class="sy0">.</span>send<span class="br0">&#40;</span><span class="kw3">file</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>In the FileUpload function we do the following: first we go thrue all the files (multiple file upload is supported), then we add the &#8216;onreadystatechange&#8217; event (this will be called when the file finishes uploading), after this we add the &#8216;onprogress&#8217; event (to show the upload progress of the file) and then we upload the file using a POST request and the &#8216;send&#8217; function from xhr2. We also send some extra parameters like the name of the file (file.fileName) and a path that we defined as an attribut for the dropzone(this is not mandatory, just an example of sending extra data).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2011/07/jquery-file-upload-with-dragdrop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ajax with Drupal callbacks</title>
		<link>http://www.using-jquery.com/2011/05/ajax-with-drupal-callbacks/</link>
		<comments>http://www.using-jquery.com/2011/05/ajax-with-drupal-callbacks/#comments</comments>
		<pubDate>Sun, 29 May 2011 10:49:01 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=274</guid>
		<description><![CDATA[In this short article I would like to explain how to create a simple AJAX request handler in Drupal. So you have a module created, and you would like to add some AJAX requests in a page, but the question is where to point the requests to? What you have to do is create a ]]></description>
			<content:encoded><![CDATA[<p>In this short article I would like to explain how to create a simple AJAX request handler in Drupal.<br />
So you have a module created, and you would like to add some AJAX requests in a page, but the question is where to point the requests to? What you have to do is create a Drupal callback, that you can use in a AJAX request, and that will return just the data you need in the callback. </p>
<p><span id="more-274"></span></p>
<p>To create a Drupal AJAX callback we need to add a hook_menu in our module, and there declare a callback, like this:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">&nbsp;<span class="re1">$items</span><span class="br0">&#91;</span><span class="st0">&#39;admin/settings/test&#39;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;page callback&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;test_function&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;type&#39;</span> <span class="sy0">=&gt;</span> MENU_CALLBACK<span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;access arguments&#39;</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;access content&#39;</span><span class="br0">&#41;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>And now in our <b> test_function </b> we can return whatever data we need, for example:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> test_function<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> json_encode<span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;output&#39;</span><span class="sy0">=&gt;</span><span class="st0">&#39;test&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So now we can point to &#8216;/settings/test&#8217; in our AJAX request.<br />
If we are using a language module then we should call the AJAX callback after passing it through the Drupal <b>url</b> function:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">&lt;?</span><span class="sy0">=</span>url<span class="br0">&#40;</span><span class="st0">&#39;admin/settings/test&#39;</span><span class="br0">&#41;</span><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p>to have the correct language set in the callback. So if german is selected, we would have &#8216;/de/settings/test&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2011/05/ajax-with-drupal-callbacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery and jsonp (cross-domain requests in jQuery)</title>
		<link>http://www.using-jquery.com/2011/02/jquery-and-jsonp-cross-domain-requests-in-jquery/</link>
		<comments>http://www.using-jquery.com/2011/02/jquery-and-jsonp-cross-domain-requests-in-jquery/#comments</comments>
		<pubDate>Sat, 12 Feb 2011 15:25:41 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=270</guid>
		<description><![CDATA[As we know AJAX is a great tool in making very nice and interactive web applications, but one of it&#8217;s drawbacks is the fact that with AJAX we can&#8217;t do cross-domain requests because of restrictions imposed by the browser (same-origin policy). To solve this problem one of the most simple and scalable solutions is to ]]></description>
			<content:encoded><![CDATA[<p>As we know AJAX is a great tool in making very nice and interactive web applications, but one of it&#8217;s drawbacks is the fact that with AJAX we can&#8217;t do cross-domain requests because of restrictions imposed by the browser (same-origin policy). To solve this problem one of the most simple and scalable solutions is to insert a script element in the page (the script element is not restricted by the same-origin policy) and get the required data this way. To make this solution easy-to-use, JSON format is used to return the data wrapped in a function call.<br />
<span id="more-270"></span><br />
So JSONP is a script tag injection, passing the response from the server in to a user specified function by using the JSON data format.</p>
<p>Ok..so let&#8217;s say we would like to make a cross-domain using jQuery. Here is how the jQuery $.ajax call should look like:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">$<span class="sy0">.</span>ajax<span class="br0">&#40;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; dataType<span class="sy0">:</span> <span class="st0">&#39;jsonp&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; data<span class="sy0">:</span> <span class="st0">&#39;id=test&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; jsonp<span class="sy0">:</span> <span class="st0">&#39;jsonp_callback&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; url<span class="sy0">:</span> <span class="st0">&#39;http://www.differentdomain.com/get_data.php&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; success<span class="sy0">:</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="co1">// do something</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>So we use the simple $.ajax method from jQuery, but we add the jsonp attribute with the value &#8220;jsonp_callback&#8221;. What this will do is add to the request that we do a parameter that is the function that the script should return the JSON data wrapped in&#8230; so the http://www.differentdomain.com/get_data.php would get at the end &#038;jsonp_callback=xxx where xxx will be a dynamicaly generated function by jQuery(something like jsonpcallback123456)</p>
<p>Now on the server side (in the get_data.php file) we have to get the callback name and send the data in JSON format wrapped in the callback function.<br />
Something like this:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$jsonp_callback</span> <span class="sy0">=</span> <span class="re1">$_GET</span><span class="br0">&#91;</span><span class="st0">&#39;jsonp_callback&#39;</span><span class="br0">&#93;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$data</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;1&#39;</span><span class="sy0">,</span><span class="st0">&#39;2&#39;</span><span class="sy0">,</span><span class="st0">&#39;3&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="re1">$jsonp_callback</span> <span class="sy0">.</span> <span class="st0">&#39;(&#39;</span><span class="sy0">.</span>json_encode<span class="br0">&#40;</span><span class="re1">$data</span><span class="br0">&#41;</span><span class="sy0">.</span><span class="st0">&#39;);&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2011/02/jquery-and-jsonp-cross-domain-requests-in-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AJAX Navigation (window.location.hash)</title>
		<link>http://www.using-jquery.com/2011/01/ajax-navigation-window-location-hash/</link>
		<comments>http://www.using-jquery.com/2011/01/ajax-navigation-window-location-hash/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 11:16:15 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=261</guid>
		<description><![CDATA[As we all know one of the great benefits of implementing AJAX is the possibility of updating content without having to navigate to a new Web page. With this comes the drawback that the user can not come back to the last action, and doesn&#8217;t have a navigation link for his AJAX generated page. The ]]></description>
			<content:encoded><![CDATA[<p>As we all know one of the great benefits of implementing AJAX is the possibility of updating content without having to navigate to a new Web page. With this comes the drawback that the user can not come back to the last action, and doesn&#8217;t have a navigation link for his AJAX generated page.</p>
<p>The solution: using window.location.hash to keep information regarding AJAX action.<br />
For example let&#8217;s say an AJAX request is done when a button is pressed in the page, and when that button is pressed the AJAX request load&#8217;s data for that buttons id, we will add required data to the window.location.hash, like this:<br />
<span id="more-261"></span></p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span><span class="st0">&#39;#book1&#39;</span><span class="br0">&#41;</span><span class="sy0">.</span>click<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"><span class="co1">//add to hash data that you need to make the AJAX request later</span></div>
</li>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span>window<span class="br0">&#41;</span><span class="sy0">.</span>location<span class="sy0">.</span>hash <span class="sy0">=</span> $<span class="br0">&#40;</span>this<span class="br0">&#41;</span><span class="sy0">.</span>attr<span class="br0">&#40;</span><span class="st0">&#39;id&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">$<span class="sy0">.</span>ajax<span class="br0">&#40;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;type<span class="sy0">:</span> <span class="st0">&quot;POST&quot;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;url<span class="sy0">:</span> current_url<span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;dataType<span class="sy0">:</span> <span class="st0">&quot;html&quot;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;data<span class="sy0">:</span><span class="st0">&#39;selection_id=&#39;</span><span class="sy0">+</span>$<span class="br0">&#40;</span>this<span class="br0">&#41;</span><span class="sy0">.</span>attr<span class="br0">&#40;</span><span class="st0">&#39;id&#39;</span><span class="br0">&#41;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;success<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span>html<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//do something</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span> <span class="br0">&#125;</span><span class="br0">&#41;</span> &nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Now on page load we need to grab the hash (if it exists and make the appropriate request), like this:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">$<span class="br0">&#40;</span>document<span class="br0">&#41;</span><span class="sy0">.</span>ready<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"><span class="kw1">if</span> <span class="br0">&#40;</span>$<span class="br0">&#40;</span>window<span class="br0">&#41;</span><span class="sy0">.</span>location<span class="sy0">.</span>hash<span class="sy0">.</span>length<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//we will use $(window).location.hash.replace(&#39;#&#39;,&#39;&#39;) in the &quot;data&quot; section of the AJAX request</span></div>
</li>
<li class="li1">
<div class="de1">$<span class="sy0">.</span>ajax<span class="br0">&#40;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;type<span class="sy0">:</span> <span class="st0">&quot;POST&quot;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;url<span class="sy0">:</span> current_url<span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;dataType<span class="sy0">:</span> <span class="st0">&quot;html&quot;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;data<span class="sy0">:</span><span class="st0">&#39;selection_id=&#39;</span><span class="sy0">+</span>$<span class="br0">&#40;</span>window<span class="br0">&#41;</span><span class="sy0">.</span>location<span class="sy0">.</span>hash<span class="sy0">.</span>replace<span class="br0">&#40;</span><span class="st0">&#39;#&#39;</span><span class="sy0">,</span><span class="st0">&#39;&#39;</span><span class="br0">&#41;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;success<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span>html<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//do something</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span> &nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>And now you can navigate to your AJAX request. </p>
<p>Also I suggest using a special character at the beginning of the hash (like &#8216;!&#8217;), because when location.hash is changed the browser tries to move (lower) the position of the document to any DOM element with the id defined in the hash, so if hash is #book1, the browser will move (lower) the page to the DOM element, so if you use &#8216;!&#8217; and have #!book1 the !book1 id doesn&#8217;t exist and you can avoid any problems.</p>
<p>Another idea is to integrate &#8216;Back&#8217; actions for navigation, so when a user presses the &#8216;Back&#8217; button of the browser, you will show the requested AJAX content. There are a couple of jQuery plugins out there had handle the hashchange event, and that you can use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2011/01/ajax-navigation-window-location-hash/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>What are javascript closures?</title>
		<link>http://www.using-jquery.com/2010/12/what-are-javascript-closures/</link>
		<comments>http://www.using-jquery.com/2010/12/what-are-javascript-closures/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 15:28:31 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=251</guid>
		<description><![CDATA[Javascript closures are considered to be advanced stuff, and not many concern about this tehnique. That&#8217;s really a shame, because closures are the answer to the problem of how to bind variables to functions that are called at a later time. I actually found the need to use this technique while working on a project. ]]></description>
			<content:encoded><![CDATA[<p>Javascript closures are considered to be advanced stuff, and not many concern about this tehnique. That&#8217;s really a shame, because closures are the answer to the problem of how to bind variables to functions that are called at a later time. I actually found the need to use this technique while working on a project.</p>
<p>A closure takes place when a created function binds local variables to it in such a way that they are kept alive after the function has returned. A closure is a special kind of object that combines two things: a function, and any local variables that were in-scope at the time that the closure was created.</p>
<p>Here is an simple example:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> say<span class="br0">&#40;</span>name<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> text <span class="sy0">=</span> name<span class="sy0">;</span> <span class="co1">// variable</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> alert<span class="br0">&#40;</span>text<span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">say<span class="br0">&#40;</span><span class="st0">&#39;Hello&#39;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p><span id="more-251"></span><br />
In JavaScript, when we declare a function within another function, then the local variables can remain accessable after returning from the called function. Above, when we call the function say(); we get returned a anonymous function, that we call &#8211; (&#8216;Hello&#8217;)() &#8211; and in that function we have access to variables in the first functions scope.</p>
<p>I know that it may not make a lot of sense but let me give you a real life example, where I had to use closures.</p>
<p>So the issue was this, I had to call a function encryptData  to which I had to give a string and a callback, that would respond with an encryption of the string that I gave, something like this:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">encryptData<span class="br0">&#40;</span>original_string<span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>encrypted<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; alert<span class="br0">&#40;</span>encrypted<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Only issue was that I wanted to also show the original string, keep in mind that i called this function multiple times for different strings, and the encrypted string didn&#8217;t necessarily come in the same order I&#8217;ve sent them.</p>
<p>So what I wanted to do is something like this(the example will not work):</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">encryptData<span class="br0">&#40;</span>original_string<span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>encrypted<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; alert<span class="br0">&#40;</span>encrypted <span class="sy0">+</span> <span class="st0">&quot; Original:&quot;</span> <span class="sy0">+</span> original_string<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Problem that in the anonymous function the original_string was not accesible anymore, so the fix was using closure, like this:</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">encryptData<span class="br0">&#40;</span>original_string<span class="sy0">,</span><span class="kw2">function</span><span class="br0">&#40;</span>original_string<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">return</span> <span class="kw2">function</span><span class="br0">&#40;</span>encrypted<span class="br0">&#41;</span><span class="br0">&#123;</span>alert<span class="br0">&#40;</span>encrypted <span class="sy0">+</span> <span class="st0">&quot; Original:&quot;</span> <span class="sy0">+</span> original_string<span class="br0">&#41;</span><span class="sy0">;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#40;</span>original_string<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Now in the anonymous function that I send as a callback I have access to the original string.And this is javascript closure.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2010/12/what-are-javascript-closures/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery and Zend Framework</title>
		<link>http://www.using-jquery.com/2010/11/jquery-and-zend-framework/</link>
		<comments>http://www.using-jquery.com/2010/11/jquery-and-zend-framework/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 15:01:38 +0000</pubDate>
		<dc:creator>Ionut</dc:creator>
				<category><![CDATA[articles]]></category>

		<guid isPermaLink="false">http://www.using-jquery.com/?p=244</guid>
		<description><![CDATA[One of the frameworks that offer a very nice support for jQuery is Zend Framework. Zend Framework is an open source framework implemented in PHP 5 by the developers of PHP. Zend framework is a highly MVC(Model–View–Controller) based framework that makes developing PHP web application much easier. For those who aren&#8217;t familiar with MVC, well ]]></description>
			<content:encoded><![CDATA[<p>One of the frameworks that offer a very nice support for jQuery is Zend Framework.<br />
Zend Framework is an open source framework implemented in PHP 5 by the developers of PHP. Zend framework is a highly MVC(Model–View–Controller) based framework that makes developing PHP web application much easier.</p>
<p><span id="more-244"></span></p>
<p>For those who aren&#8217;t familiar with MVC, well MVC is a application architecture that consists in separating any application by 3 major components: Model(manages the behavior and data of the application), View(creates/defines the visual interface), Controller (is the middle men between the input and the models, receives input and initiates a response by making calls on model objects).</p>
<p>Zend framework integrated the use of jQuery in the View, so jQuery can be used very easy in the View templates.<br />
First jQuery is added to the view helper path like this: </p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1">view<span class="sy0">-&gt;</span><span class="me1">addHelperPath</span><span class="br0">&#40;</span><span class="st0">&quot;ZendX/JQuery/View/Helper&quot;</span><span class="sy0">,</span> <span class="st0">&quot;ZendX_JQuery_View_Helper&quot;</span><span class="br0">&#41;</span><span class="sy0">.</span></div>
</li>
</ol>
</div>
<p>Now we can crete simple Ajax links :</p>
<div class="geshi php">
<ol>
<li class="li1">
<div class="de1"><span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">ajaxLink</span><span class="br0">&#40;</span><span class="st0">&quot;A link&quot;</span><span class="sy0">,</span> <span class="st0">&quot;/hello/world&quot;</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;update&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;#content&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p><em>In view the javascript library must be loaded.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.using-jquery.com/2010/11/jquery-and-zend-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

