localStorage
by Avi Algaly
HTML
<form>
<input type="date" name="dat" id="dat" />
<input type="submit" value="store this date" />
</form>
<input type="button" value="show converted dates" id="sd" />
<div id="conversion">nothing</div>
JavaScript
window.store = {
localStoreSupport: function () {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}(),
set: function (name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else {
var expires = "";
}
if (this.localStoreSupport) {
localStorage.setItem(name, value);
} else {
document.cookie = name + "=" + value + expires + "; path=/";
}
},
get: function (name) {
if (this.localStoreSupport) {
return localStorage.getItem(name);
} else {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
},
del: function (name) {
if (this.localStoreSupport) {
localStorage.removeItem(name);
} else {
this.set(name, "", -1);
}
}
};
function dmy() {
var mydate = document.getElementById('dat').value,
yf = mydate.split("-")[0],
mf = mydate.split("-")[1],
df = mydate.split("-")[2],
b = store.get("b");
if (!b) b = [];
else b = JSON.parse(b);
b.push({
df: df,
mf: mf,
yf: yf
});
store.set("b", JSON.stringify(b));
return false;
}
function showdate() {
var sdate = JSON.parse(store.get('b')),
a = '';
if (sdate != null) for (var i = 0; i < sdate.length; i++)
a += sdate[i]['df'] + '/' + sdate[i]['mf'] + '/' + sdate[i]['yf'] + '<br>';
...