Twitter: raymondcamden


Address: Lafayette, LA, USA

LocalStorage Example: Storing previous searches

07-13-2012 6,775 views jQuery, JavaScript, HTML5, ColdFusion 4 Comments

I've made it clear that I'm a huge fan of LocalStorage. While sitting in the Houston airport for many hours this week, I decided to whip up a little example that demonstrates one of the practical uses for this feature. I built a simple Ajax-based search page and then added LocalStorage as a way to remember your previous searches. This isn't anything special, but I think it's another good example of the feature. Let's begin by taking a look at the application before any use of LocalStorage.

The application makes use of Bootstrap, jQuery, and Handlebars, but for now, let's just focus on the JavaScript and the template:

You can see that I begin by compiling my Handlebars template (this is used for the results), and then I define my click handler for the search button. All the handler does is grab the search string, pass it to a ColdFusion service that hits the database, and then passes the results to my Handlebars template.

Beautiful - and the code is rather simple. You can test this yourself here: http://www.raymondcamden.com/demos/2012/jul/13/test1.html I'd recommend searching for oil, moon, paint, and beer.

Ok, now let's talk about the next version. The updated version will use LocalStorage to remember your last five searches. I decided on five mostly because it just felt right. I thought ten might be a bit much.

To store the last five values, I'll use an array. You can't store complex variables in LocalStorage, but you can easily serialize them with JSON. So for example:

That isn't too complex, is it? Storing the value isn't too hard either. I do a check to see if the value exists in the array, and if not, put it in the front and pop one off the end if the array is too big.

All that's left then is to simply write out the past searches and listen for click events on them.

And voila - I can now remember your last five searches and provide an easy way for you to quickly rerun them. The code samples above are only the most important bits. I encourage you to View Source on the updated version for the complete example. (The ColdFusion code is just a simple query API. You can view that template here.)

4 Comments

  • Commented on 07-21-2012 at 2:39 AM
    It's great to see a little app that uses jQuery, Ajax, and Local Storage with just a one line of reference for the ColdFusion component. I'd like to start using HTML 5 features like Local Storage, do you find you are using this a lot? Do you use anything beyond Firebug to troubleshoot using it? Great read, thanks!
  • Commented on 07-22-2012 at 9:24 PM
    Do I use LocalStorage a lot? I suppose so. Remember, I don't have a 'real' job now. I do a lot of these "Proof of Concept" posts so I try a bunch of different things. But yeah - LocalStorage is probably one of the easiest HTML5 features to use.

    As for troubleshooting, I normally use Chrome's DevTools instead of Firebug.
  • Chris Larsen #
    Commented on 05-16-2013 at 3:43 PM
    Do you know if localStorage and be accessed server-side similar to how cookies can be accessed server-side?
  • Commented on 05-17-2013 at 6:20 AM
    Nope, it cannot. You can, however, use AJAX to send localStorage data to a server.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty