Storing data on the browser

Cookies have some pretty severe limitations in terms of the amount of data you can store. Besides, they get transmitted to the server on every request, so you don’t necessarily want to store a ton of data in there anyway.

On the other hand, using sessions on the server to store data for the current user is also a bad idea unless you have a small or very predictable user base. If you’ve got a heavily dynamic app and you want to scale it up somewhat linearly, you need to work with HTTP’s generally stateless model.

The Ajaxian blog points to a couple of posts by Brad Neuberg about keeping session state in the browser. Using AJAX and Brad’s techniques, you’re only sending the state back to the server when you need to, not every time as you do with cookies.

The quick trick (it’s worth linking through to Brad’s original article) is to store your “session” data in a text field in a form. (It must be in a form.) Brad tested in IE, Firefox and Safari and found that he could store data in all three, and it would still be there when he hit the back button. Additionally, he tried storing 1MB in both IE and Firefox and had no problem.

That can certainly solve session affinity problems, couldn’t it?

4 thoughts on “Storing data on the browser”

  1. Maybe it is common knowledge, I dunno. My last job was largely in Swing, and the one before that was in the dark days of requiring Netscape 4.7 support. That, and the requirement to support people who don’t have JavaScript on. It’s just not something I had encountered previously.

  2. With regards to the second half of his article (“a crazy hack”), I can’t get the demo to work in either Firefox 1.0.6 or IE 6. I think he’s smokin’ something illegal.

    Yes, the data is there when you hit the back button, but not when you leave the page and come back — as he claims. I tried it and cannot replicate his results in either Firefox 1.0.6 or IE 6.

    1. Goto and see the alert box “Storing new session value”

    2. Type in the address bar

    3. Type in the address bar. I again see the “Storing new session value” alert box.

    Server-side sessions are required for HTTP 1.1.

  3. The technique of storing data in a form will most definitely *not* work between separate visits to the site. I think the author’s use of the phrase “between page loads” doesn’t quite coincide with the rest of his text which talks about the back/forward buttons and history.

    For me, the interesting bit was the proper support for the back button plus the ability to go past the 4K cookie limit. (And, unlike cookies, you don’t have to send all of the data on every request… just the stuff that interests that particular request.)

Comments are closed.