Skip to content

Instantly share code, notes, and snippets.

@iegorov
Created August 14, 2013 03:12
Show Gist options
  • Save iegorov/6227709 to your computer and use it in GitHub Desktop.
Save iegorov/6227709 to your computer and use it in GitHub Desktop.
session storage
var sessionStorage;
$(function () {"use strict";
if (typeof sessionStorage === 'undefined') {
sessionStorage = (function () {
var data = window.top.name ? JSON.parse(window.top.name) : {};
return {
clear: function () {
data = {};
window.top.name = '';
},
getItem: function (key) {
return data[key] || null;
},
key: function (i) {
// not perfect, but works
var ctr = 0,
k;
for (k in data) {
if (ctr === i) {
return k;
} else {
ctr += 1;
}
}
},
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);
}
};
})();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment