HTML and CSS Reference
In-Depth Information
getItem: function (key) {
return data[key] || null;
},
key: function (i) {
// not perfect, but works
var ctr = 0;
for (var k in data) {
if (ctr == i) return k
else ctr++;
}
},
removeItem: function (key) {
delete data[key];
window.top.name = JSON.stringify(data);
},
setItem: function (key, value) {
data[key] = value+''; // forces the value to a
¬ string
window.top.name = JSON.stringify(data);
}
};
})();
}
The problem with implementing sessionStorage manually (as
shown in the previous code listing) is that we don't have the setters
and getters (or rather IE7 and below doesn't support setters and
getters and those are really the only widely deployed old brows-
ers that lack native sessionStorage ). This means you couldn't write
sessionStorage.twitter = '@rem' . Although technically, the code
would work, it wouldn't be registered in our storage object properly
and sessionStorage.getItem('twitter') wouldn't yield a result.
Yo u c o u l d g e t a r o u n d t h a t p a r t i c u l a r p r o b l e m , b u t m o s t i m p o r t a n t l y,
refreshing the browser would lose the data.
With this in mind, and depending on what browsers you are target-
ing (that is, whether you would need to provide a manual fallback
to storage), you may want to agree with yourself for safety as to
when you should stick to using the setItem and getItem methods.
 
Search WWH ::




Custom Search