<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2009://34.37351-</id>
  <updated>2009-11-16T14:53:05Z</updated>
  <title>Comments for jQuery Example - Using cookies to save draft information (http://www.insideria.com/2009/06/jquery-example---using-cookies.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2009://34.37351</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=37351" title="jQuery Example - Using cookies to save draft information" />
    <published>2009-07-01T01:41:24Z</published>
    <updated>2009-07-01T02:34:59Z</updated>
    <title>jQuery Example - Using cookies to save draft information</title>
    <summary>An example of using jQuery to store draft information in cookies.</summary>
    <author>
      <name>Raymond Camden</name>
      <uri>http://www.coldfusionjedi.com</uri>
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[As much as I love my browser (Firefox 3.5 FTW!), I'm no stranger to quirkiness, crashes, and just general breakdowns when surfing the web. As a blogger, this can be especially frustrating. Nothing bugs me more than losing a good blog entry because of a crash or some other (normally user) mistake. For a while now I've employed a simple method on my own blog to handle this possibility. Every few seconds I use cookies to store the values of the blog entry and blog title. If for some reason the browser crashes (or the operating system, or I lose power, etc.), I can come back to the page and see the contents of my blog entry just as if I've never left. 
<br/><br/>
This method is not perfect of course. There is a strict limit to the size of the cookie data you store on the browser. You have a limit not only on the total number of cookies but to how much data can be stored. In my simple testing, I've never seen a blog entry that didn't "fit" inside the cookie. I'm not writing a novel, nor should I be on a blog. 
<br/><br/>
So with that being said, here is a simple example of this technique that makes use of both jQuery, and a very nice, very simple, <a href="http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/">jQuery Cookie Plugin</a>.  This plugin makes cookie manipulation as easy as $.cookie('name','val'), which is about as easy as you can make it. I'll display the entire page, and then walk through the functionality.
<br/><br/>
<div class="acode" style="overflow: auto; padding: 10px; height: 240px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
&lt;<span class="category2">html</span>&gt;

&lt;head&gt;
&lt;script src="<span class="quote">jquery/jquery.js</span>"&gt;&lt;/script&gt;
&lt;script src="<span class="quote">jquery/jquery.cookie.js</span>"&gt;&lt;/script&gt;
&lt;script&gt;

$(document).ready(<span class="category1">function</span> () {
 
 	console.<span class="category2">log</span>('<span class="quote">Start me up!</span>')
 	<span class="linecomment">//check for draft_title and draft_body</span>
 	<span class="category1">var</span> draftTitle = $.cookie('<span class="quote">draftTitle</span>')
 	<span class="category1">var</span> draftBody = $.cookie('<span class="quote">draftBody</span>')
 
 	console.<span class="category2">log</span>('<span class="quote">title=</span>'+draftTitle)
 	console.<span class="category2">log</span>('<span class="quote">body=</span>'+draftBody)
 
 	<span class="category1">if</span>(draftTitle) $("<span class="quote">#title</span>").val(draftTitle)
 	<span class="category1">if</span>(draftBody) $("<span class="quote">#body</span>").val(draftBody)
 
 	<span class="linecomment">//update the cookies every 10 seconds....</span>
 	window.<span class="category1">setInterval</span>('<span class="quote">saveDraft()</span>', 5*1000)
 
 	<span class="linecomment">//clear cookies on form submission</span>
 	$("<span class="quote">#mainForm</span>").submit(<span class="category1">function</span>() {
  		console.<span class="category2">log</span>('<span class="quote">clearing cookies...</span>')
  		$.cookie('<span class="quote">draftTitle</span>',<span class="category1">null</span>)
  		$.cookie('<span class="quote">draftBody</span>',<span class="category1">null</span>)
  		<span class="category1">return</span> <span class="category1">true</span>
  	})
 
})

<span class="category1">function</span> saveDraft() {
 	console.<span class="category2">log</span>('<span class="quote">saveDraft</span>')
 	$.cookie('<span class="quote">draftTitle</span>', $("<span class="quote">#title</span>").val(), { expires:7})
 	$.cookie('<span class="quote">draftBody</span>', $("<span class="quote">#body</span>").val(), { expires:7})
}

&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;form <span class="category1">method</span>="<span class="quote">post</span>" id="<span class="quote">mainForm</span>"&gt;
&lt;table&gt;
	&lt;tr&gt;
		&lt;td&gt;Title:&lt;/td&gt;
		&lt;td&gt;&lt;<span class="category2">input</span> <span class="category2">type</span>="<span class="quote">text</span>" <span class="category2">name</span>="<span class="quote">title</span>" id="<span class="quote">title</span>"&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;Entry:&lt;/td&gt;
		&lt;td&gt;&lt;textarea <span class="category2">name</span>="<span class="quote">body</span>" id="<span class="quote">body</span>" cols="<span class="quote">40</span>" rows="<span class="quote">10</span>"&gt;&lt;/textarea&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td&gt;&lt;<span class="category2">input</span> <span class="category2">type</span>="<span class="quote">submit</span>" value="<span class="quote">Save</span>"&gt;&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/<span class="category2">html</span>&gt;</pre>
</code>

</div></div>
<br/><br/>
Alright, so what's going on here? First note the form at the bottom. It mimics a simple blog entry with a title and entry. Blog entries will typically have a bit more, but these are the fields I really care about. 
<br/><br/>
Scrolling back to the top, I've included both jQuery and the plugin. Within the document.ready block are a few separate things. The first thing I do is grab the value from two cookies - draftTitle and draftBody. If the values are not null, I use them to update the form fields. Next - I set up an interval to run saveDraft() every 5 seconds. I kind of grabbed that number out of the air, but I don't see anything wrong with it. 
<br/><br/>
The last part of the document.ready block handles form submissions. In my case the only thing I've done is clear the cookies (by setting their values to null) and then allowing the form to submit as is. 
<br/><br/>
The very last part of the code block, saveDraft, simply handles the timed interval which will store the cookie values. Note the option structure sets an expires value of 7 days. Again, this is pretty arbitrary. I assume that if your browser does crash while editing, you are most likely going to jump right back into editing pretty soon. 
<br/><br/>
Any comments on this method?]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.37351-comment:2067573</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.37351" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html#comment-2067573" />
    <title>Comment from mike on 2009-07-01</title>
    <author>
        <name>mike</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Nice post, Raymond.  One comment on the script:</p>

<p>It would be more efficient to change this line:</p>

<p> 	window.setInterval('saveDraft()', 5*1000)</p>

<p>to this:</p>

<p> 	window.setInterval(saveDraft, 5*1000)</p>

<p>And then you can move the saveDraft function declaration to be inside your document-ready callback function so that it is not in the global namespace.</p>]]>
    </content>
    <published>2009-07-01T11:56:53Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.37351-comment:2067574</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.37351" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html#comment-2067574" />
    <title>Comment from Raymond Camden on 2009-07-01</title>
    <author>
        <name>Raymond Camden</name>
        <uri>http://www.coldfusionjedi.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.coldfusionjedi.com">
        <![CDATA[<p>Dang it - I knew that syntax was a bit off. I had looked it up because I was having trouble getting it working, and it just felt.... wrong to me. Thanks for confirming this for me!</p>]]>
    </content>
    <published>2009-07-01T12:06:33Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.37351-comment:2067663</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.37351" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html#comment-2067663" />
    <title>Comment from William on 2009-07-02</title>
    <author>
        <name>William</name>
        <uri>http://williamantonio.wordpress.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://williamantonio.wordpress.com">
        <![CDATA[<p>Man, Beaultiful Article!! Thanks!</p>

<p><br />
</p>]]>
    </content>
    <published>2009-07-02T12:25:57Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.37351-comment:2067688</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.37351" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html#comment-2067688" />
    <title>Comment from Trevor on 2009-07-02</title>
    <author>
        <name>Trevor</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Just a thought... when you handle the form submit event, you're clearing the cookie and then returning true. That will in effect clear your cookies before confirming whether your post submits successfully. </p>

<p>Maybe it would be better to submit the form with ajax and clear the cookies on success rather than whether it succeeds or not?</p>

<p>Am I missing something?</p>

<p>Btw, I like the cookie plugin. I'll have to try that out.</p>]]>
    </content>
    <published>2009-07-02T18:15:48Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.37351-comment:2067690</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.37351" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html#comment-2067690" />
    <title>Comment from Raymond Camden on 2009-07-02</title>
    <author>
        <name>Raymond Camden</name>
        <uri>http://www.coldfusionjedi.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.coldfusionjedi.com">
        <![CDATA[<p>@Trevor: Ah, good point. In my case, I assumed that any "trouble" would occur WHILE editing, not after I click save. I didn't want to turn the form itself into an ajax form so I kept that part simple. </p>

<p>You could remove that code, and clear the cookies server side on successful addition.</p>]]>
    </content>
    <published>2009-07-02T18:53:57Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.37351-comment:2067692</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.37351" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html#comment-2067692" />
    <title>Comment from Paul on 2009-07-02</title>
    <author>
        <name>Paul</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>That's a lot of work for limited functionality :).  Have you seen the Lazarus plugin for Firefox?  <a href="https://addons.mozilla.org/en-US/firefox/addon/6984">https://addons.mozilla.org/en-US/firefox/addon/6984</a><br />
</p>]]>
    </content>
    <published>2009-07-02T19:03:06Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.37351-comment:2067693</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.37351" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html#comment-2067693" />
    <title>Comment from Raymond Camden on 2009-07-02</title>
    <author>
        <name>Raymond Camden</name>
        <uri>http://www.coldfusionjedi.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.coldfusionjedi.com">
        <![CDATA[<p>A lot of work? I don't think so. :) If you remove all the console.logs, it's maybe 10 lines of JS. It requires no plugin for the user, and, obviously, would work in Safari/IE as well. :)</p>]]>
    </content>
    <published>2009-07-02T19:12:23Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.37351-comment:2067881</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.37351" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/06/jquery-example---using-cookies.html#comment-2067881" />
    <title>Comment from Jim on 2009-07-07</title>
    <author>
        <name>Jim</name>
        <uri>http://www.jaaulde.com/</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.jaaulde.com/">
        <![CDATA[<p>I love the idea, and things this this are what I had in mind when I added the jQuery bindings to my own JavaScript cookie library.  They help automate some of the form-to-cookie work and vice-versa.</p>

<p>The library can be found at <a href="http://plugins.jquery.com/project/cookies">http://plugins.jquery.com/project/cookies</a> or directly at <a href="http://code.google.com/p/cookies/">http://code.google.com/p/cookies/</a></p>

<p>Jim</p>]]>
    </content>
    <published>2009-07-07T14:41:16Z</published>
  </entry>

</feed
