Created
August 14, 2013 03:12
-
-
Save iegorov/6227709 to your computer and use it in GitHub Desktop.
session storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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