开发者

Different browser behaviour between localStorage/sessionStorage and cookies

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.

For example:

  • In Firefox 3.6, sessionStorage is not shared between tabs within the same browser session.
  • localStorage can never be set to expire, while sessionStorage can never persist.

  • If a site uses cookies for the session, they are shared between tabs.

  • If a cookie is set to expire, then it expires after a certain date.

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.

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 cookie-less domain, so this isn't a motivator).

To do this, we have creat开发者_StackOverflow中文版ed 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 Crockfords JSON plugin.

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.

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(!).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜