title: Different browser behaviour between localStorage/sessionStorage and cookies
body: <p>This is not so much a technical question as a question of practice: browser storage doesn't seem to have the same browser behaviour as cookies.</p>
  
  <p>For example:</p>
  
  <ul>
  <li>In Firefox 3.6, sessionStorage is not shared between tabs within the same browser session.</li>
  <li><p>localStorage can never be set to expire, while sessionStorage can never persist.</p></li>
  <li><p>If a site uses cookies for the session, they are shared between tabs.</p></li>
  <li>If a cookie is set to expire, then it expires after a certain date.</li>
  </ul>
  
  <p>It makes sense that it is not 'like for like', however this means that we may need to mix and match our variable storage solutions depending on need. Your thoughts and opinions on the benefits/pitfalls of either would be appreciated.</p>
  
  <p>Background:
  On the website we are currently developing, we decided to implement progressive enhancement using browser storage (sessionStorage and localStorage) with a cookie fall-back. Our reason for doing this is simply as a learning exercise, but moving forward this should help us on mobile platforms and situations where cookies are not usable (we are already storing our assets on a <a href="http://code.google.com/speed/page-speed/docs/request.html#ServeFromCookielessDomain" rel="nofollow">cookie-less domain</a>, so this isn't a motivator).</p>
  
  <p>To do this, we have created a class which has set, get, remove and clear methods which store the application variables within a JSON string (this allows us to group values in a hierarchy). For browsers which don't have a JSON parser we use <a href="http://www.json.org/js.html" rel="nofollow">Crockfords JSON plugin</a>.</p>
  
  <p>At run time the storage class works out if the user's browser supports browser storage, otherwise sets the functions to write to cookies instead. Due to the inconsistencies already highlighted (plus others I am unaware of), this 'like for like' progressive enhancement is actually false.</p>
  
  <p>Edit: Another browser vaguary: if IE 8 is running in IE 7 standards mode it supports localStorage and sessionStorage, where standalone IE 7 does not(!).</p>