HTML and CSS Reference
In-Depth Information
Using
clear
removes all entries, clearing the entire storage
object. For example
sessionStorage.setItem('remy', “Master of the Universe”);
sessionStorage.setItem('bruce', “Master of the Puniverse”);
alert( sessionStorage.length ); // shows 2
sessionStorage.removeItem('bruce');
alert( sessionStorage.length ); // show 1
sessionStorage.clear();
alert( sessionStorage.length ); // shows 0
Storing more than strings
Yo u c a n w of r k a r of u n d t h e s t r i n g i f y i n g of f of b j e c t s b y m a k i n g u s e of f
JSON. Since JSON uses text to represent a serialised JavaScript
object, we can use this to store objects and convert stored data
back into objects. However, it would require putting a wrapper on
the
set
and
get
methods, which (depending on your application)
may not be a problem at all.
All the latest browsers (either nightly or final releases) support
native JSON encoding using the
JSON.parse
and
JSON.stringify
methods. For those browsers that don't have JSON support,
we can include Douglas Crockford's JSON library (available at
Now you can convert your data storage and retrieval with JSON
as follows:
var videoDetails = {
author: 'bruce',
description: 'how to leverage synergies',
rating: '-2'
};
NoTE
JSON (JavaScript
Object Notation) is a text
based open standard for repre-
senting data. The specification
found at
http://json.org
is so
simple it actually fits on the back
of a business card!
sessionStorage.setItem('videoDetails', JSON.
stringify(videoDetails) );
// later on, as in page reloads later, we can extract the
¬ stored data
var videoDetails = JSON.parse(sessionStorage.getItem
¬ ('videoDetails'));