Ashish
Ashish

Reputation: 2564

jQuery to store data for sessions

I am trying to use jQuery AJAX. What my requirement is, i wish to load user names from DB in dataset, convert it to JSON format and store it in memory or using jQuery data for use while a user is browsing my site, i.e for a session. This way I can use autocomplete or my own code to display data to user.

Can anyone help me design such a scenario?

Upvotes: 1

Views: 5680

Answers (6)

josh3736
josh3736

Reputation: 145162

I've been using PersistJS with great success to do pretty much what you're describing. It's a lightweight plugin (< 10k) that abstracts away client-side storage using any of these available backends:

  • flash: Flash 8 persistent storage.
  • gears: Google Gears-based persistent storage.
  • localstorage: HTML5 draft storage.
  • whatwg_db: HTML5 draft database storage.
  • globalstorage: HTML5 draft storage (old spec).
  • ie: Internet Explorer userdata behaviors.
  • cookie: Cookie-based persistent storage.

It transparently picks the best storage backend based on browser capability and installed plugins. It works with:

  • globalStorage: Firefox 2.0+, Internet Explorer 8
  • localStorage: development WebKit
  • openDatabase: Safari 3.1+
  • userdata behavior: Internet Explorer 5.5+

...and optionally falls back to cookies if none of the other persistent storage options work. The DOM storage methods get you at least a few megabytes worth of storage space; you'll be limited to roughly 4kB if you fall back to cookies.

Upvotes: 0

John K
John K

Reputation: 28917

HTML 5 Web Storage

think beyond cookies, Google Gears and proprietary solutions

Standardized, no special plugin required. Scriptable through JavaScript/jQuery. Although supported only in the latest browsers, you can use HTML 5 Web Storage, namely localStorage and sessionStorage properties, intended to keep state on the client side in context of the website between page requests. Holds a heck of a lot more than cookies. For example IE 8 DOM storage spec (10 MB client-side storage), Firefox DOM storage spec.

Upvotes: 4

Peter McG
Peter McG

Reputation: 19045

There is also the YUI 2 Storage Utility which abstracts the storage for you (HTML 5, Google Gears, SWF) depending on what the browser supports:

The Storage Utility provides a mechanism for storing significant amounts of textual data, client-side, whether or not your browsers supports the proposed HTML 5 Storage specification.

Upvotes: 1

Zack
Zack

Reputation: 2284

Google Gears is the only solution I know of for doing this, but it requires the user to install it and only runs on a few browsers. You could, however, use $.getJSON to fetch a list of users, JSON-encoded and gzipped, from your server when the user say... focuses the auto-completed search box, then use aggressive client-side resource caching to reduce the number of hits on your database.

If it's a pretty long list of users, you may want to look into using memcached to store the list, or perhaps writing it to a file and having it be served directly by your front-end server (i.e. Nginx, Lighttpd, Apache: pretty much anything that avoids running a DB query)

Upvotes: 0

jcolebrand
jcolebrand

Reputation: 16035

Sessions means serverside. jQuery means clientside. You can't translate between the two. Cookies is the best you can hope for to remain strictly clientside.

Upvotes: 0

DMin
DMin

Reputation: 10363

JQuery data may not be useful because you will lose the data when you go from one page to the next as the page is refreshed, you can however do this using COOKIES -- there is a jQuery cookies plugin that will make it easier for you.

Upvotes: 0

Related Questions