function SaveItem() {
var name = document.forms.ShoppingList.name.value;
var data = document.forms.ShoppingList.data.value;
localStorage.setItem(name, data);
doShowAll();
}
function ModifyItem() {
var name = document.forms.ShoppingList.name.value;
document.forms.ShoppingList.data.value = localStorage.getItem(name);
doShowAll();
}
function RemoveItem() {
var name = document.forms.ShoppingList.name.value;
document.forms.ShoppingList.data.value = localStorage.removeItem(name);
doShowAll();
}
function ClearAll() {
localStorage.clear();
doShowAll();
}
// dynamically draw the table
function doShowAll() {
if (CheckBrowser()) {
var key = "";
var list = "
Name | Value |
\n";
var i = 0;
for (i = 0; i <= localStorage.length - 1; i++) {
key = localStorage.key(i);
list += "" + key + " | \n"
+ localStorage.getItem(key) + " |
\n";
}
if (list == "Name | Value |
\n") {
list += "empty | \nempty |
\n";
}
document.getElementById('list').innerHTML = list;
} else {
alert('Cannot store shopping list as your browser do not support local storage');
}
}
/*
* Checking the browser compatibility.
*
* Alternately can use Modernizr scripts- JavaScript library that helps us to
* detect the browser support for HTML5 and CSS features Example -
*
* if (Modernizr.localstorage) { //use localStorage object to store data } else {
* alert('Cannot store user preferences as your browser do not support local
* storage'); }
*/
function CheckBrowser() {
if ('localStorage' in window && window['localStorage'] !== null) {
// we can use localStorage object to store data
return true;
} else {
return false;
}
}