Showing posts with label webstorage. Show all posts
Showing posts with label webstorage. Show all posts

Friday, October 28, 2016

Chrome Cookies

chrome://settings/content provides customized settings for Cookies (as well as Storage)

Cookies are files created by websites you've visited. Cookies store browsing information, like your site preferences or profile information. There are two types of cookies:

First-party cookies are set by the site shown in the address bar.
Third-party cookies come from other sites that have things like ads or images embedded on the page you're visiting.

Allow local data to be set (recommended) - allow first-party and third-party cookies and web storage
Keep local data only until you quit your browser - Chrome allows sites to keep information that includes first-party and third-party cookies and web storage while you're browsing, but erase them when you quit Chrome
Block sites from setting any data - block first-party and third-party cookies and web storage, usually Chrome will throw a security error
"Uncaught SecurityError: Failed to read the 'sessionStorage' property from 'Window': Access is denied for this document
Block third-party cookies and site data - allow only first-party cookies and web storage, but block all third-party cookies and web storage

https://support.google.com/chrome/answer/95647

Monday, March 9, 2015

WebStorage fallback

http://caniuse.com/#search=webstorage shows WebStorage is well supported across browsers, but modern browsers support private browsing mode and the implementation is quite different with regarding to WebStorage support. For example, Chrome is doing a very smart logic - support localStorage and sessionStorage, but stores the data in a temp folder. On the other hand, Safari is doing a dumb logic - disable storage APIs in private browsing mode.

Nowadays Web developers don't need to worry about IE6 any more, but need to think of private browsing experience. As to writing, most browsers have private browsing mode though their names are slightly different.

IE: InPrivate browsing
Firefox: New Private window
Safari: New Private window
Chrome: Incognito window

To better support private browsing mode, we need fallback (just like we did for old browsers or new ES6 features). Googled for some time and found cookie is one possible solution to fallback Storage APIs.

https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills
https://gist.github.com/remy/350433
http://stackoverflow.com/questions/4692245/html5-local-storage-fallback-solutions
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage

It works in some cases, but cookie has issues:
1. cookie value has size limit
2. cookie is shared across tab/windows
3. cookie has http overhead
4. cookie has security issues
5. cookie value is string instead of JSON

Monday, April 14, 2014

Private browsing will cause QUOTA_EXCEEDED_ERR

If your web site uses local storage, and when user does private browsing your website, it might break. The reason is local storage will not be able to store anything if phone is using private browsing.

Suggest to use Modernizr or below script to do detect. (Try-catch might also work?)

if (!!window.localStorage)
{
    localStorage.setItem(key, val);
};


Friday, October 18, 2013

7 browser client side storage

This blog is very good and useful for reference.

http://www.sitepoint.com/html5-browser-storage-past-present-future/


  1. JavaScript Variables (Past, Present and Future)
  2. Cookies (Past, Present and Future)  SSL/HttpOnly
  3. window.name (Past and Present)
  4. HTML5 Web SQL Database (Past)
  5. HTML5 Web Storage (Present and Future) sessionStorage/localStorage
  6. HTML5 IndexedDB (Future)
  7. HTML5 File API (Future)

Thursday, September 5, 2013

QUOTA_EXCEEDED_ERR: DOM Exception 22

I met this error when set data to sessionStorage. It means the data exceeded the browser limit. Most browsers have 2.5M or 5M limit per domain. Many developers assume that one character equals one byte, but this is not a safe assumption. Strings in JavaScript are UTF-16, so each character requires two bytes of memory. This means that while many browsers have a 5 MB limit, you can only store 2.5 M characters.

http://dev.w3.org/html5/webstorage/#disk-space recommends a 5M limit, but as of writing, Chrome 29, Safari 6 still use 2.5M limit, Firefox/Opera are ok. Also Firefox/Opera allows user to adjust the default value (about:config - search quota), Chrome/Safari/IE do not allow.

The practice is to handle this error in Javascript, and fallback to browser memory.

The error might look like as below
Uncaught QuotaExceededError: An attempt was made to add something to storage that exceeded the quota.

Browser storage

There are the following storage types in browser side when do Javascript based web app.
  • Cookie: there is # of cookies per domain limit, and size limit (around 4K limit)
  • Memory: limit by system (?), data lost during browser refresh
  • WebStorage: (sessionStorage, or localStorage) - http://diveintohtml5.info/storage.html
  • AppCache:  It is to store the whole web-app (pages, images, css, JavaScript) in the browser to make it available offline.
  • Web SQL: relational database, SQLlite on browser, deprecated
  • IndexedDB: NoSQL DB on browser side. IndexedDB which is already available on Chrome 12+ and Firefox 5+, and, soon, in IE 10 as well.
  • GlobalStorage - deprecated