localStorage is also called Web Storage or DOM storage. It is a database that can store only key-value pairs, and the value data-type should always be a string. At first, this might seem a serious limitation, but using along with JavaScript arrays and JSON we can achieve quite a bit. The good news is that this feature is supported in the latest versions of all browsers, both on desktop and mobile environments (Android and iOS). Also, the developer gets a huge 5 MB of storage per domain.
localStorage support on browsers/platforms
This table lists the minimum version of each browser required to support localStorage:
| IE | Firefox | Safari | Chrome | Opera | iPhone | Android |
| 8.0+ | 3.5+ | 4.0+ | 4.0+ | 10.5+ | 2.0+ | 2.0+ |
Operations on localStorage
The methods of
localStorage include setItem(), getItem(), removeItem() and clear(). Here is sample code to add a record to localStorage using setItem():// max limit reached exceptiontry { localStorage.setItem(itemId, "Sample Value");} catch (e) { if (e == QUOTA_EXCEEDED_ERR) { alert('Quota exceeded!'); }} |
We will use
getItem() and removeItem() in the Web app that we will develop. Note thatlocalStorage.clear() removes all entries from the database, so use it with caution!
No comments:
Post a Comment