HTML and CSS Reference
As a guidance for space, Web Storage typically has a limit of
5Mb (but Safari, for instance, will prompt users if more than 5Mb
is used, and ask whether they want to allow the website to go
beyond the current default).
On the other side of the fence, the Web SQL Databases specifi-
cation doesn't talk about limits, and it's up to the author to try to
gauge an idea of the total size of the database when it's created.
In both cases, the browser will throw an error if the API wasn't
able to write the data, but I'll focus on smaller applications
where the data stored is around the 100Kb mark.
In a nutshell, the Web Storage API is cookies on steroids. One key
advantage of this API is that it splits session data and longterm
data properly. For contrast, if you set a “session” cookie (that is,
one without expiry data), that data item is available in all windows
that have access to that domain until the browser is shut down.
This is a good thing, because the session should last the time the
window is open, not the browser's lifetime. The storage API offers
two types of storage: sessionStorage and localStorage .
If you create data in sessionStorage , it's available only to that
window until the window is closed (for example, when the ses-
sion has ended). If you opened another window on the same
domain, it wouldn't have access to that session data. This is
useful to keep a shopping session within one window, whereas
cookies would let the session “leak” from one window to
another, possibly causing the visitor to put through an accidental
localStorage is based around the domain and spans all win-
dows that are open on that domain. If you set some data on
local storage it immediately becomes available on any other
window on the same domain, but also remains available until
it is explicitly deleted either by you as the web author or by
the user. Otherwise, you can close your browser, reboot your
machine, come back to it days later, and the data will still be
there. Persistent data without the hassle of cookies: having to
reset the expiry again and again.
NOTE Cookies on
steroids vs. regular cook-
ies: IE6 supports only 20 cookies
per domain and a maximum size
of 4K per cookie. Web Storage
doesn't have a limit per domain
and limits per domain are
upwards of 5Mb.