mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-04-06 10:45:53 +00:00
add localStorage polyfill for IE < 8
This commit is contained in:
parent
675b0b0c79
commit
085cf1d7f6
4 changed files with 95 additions and 0 deletions
92
assets/storage-polyfill.js
Normal file
92
assets/storage-polyfill.js
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
if (!window.localStorage || !window.sessionStorage) (function () {
|
||||
|
||||
var Storage = function (type) {
|
||||
function createCookie(name, value, days) {
|
||||
var date, expires;
|
||||
|
||||
if (days) {
|
||||
date = new Date();
|
||||
date.setTime(date.getTime()+(days*24*60*60*1000));
|
||||
expires = "; expires="+date.toGMTString();
|
||||
} else {
|
||||
expires = "";
|
||||
}
|
||||
document.cookie = name+"="+value+expires+"; path=/";
|
||||
}
|
||||
|
||||
function readCookie(name) {
|
||||
var nameEQ = name + "=",
|
||||
ca = document.cookie.split(';'),
|
||||
i, c;
|
||||
|
||||
for (i=0; i < ca.length; i++) {
|
||||
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;
|
||||
}
|
||||
|
||||
function setData(data) {
|
||||
data = JSON.stringify(data);
|
||||
if (type == 'session') {
|
||||
window.top.name = data;
|
||||
} else {
|
||||
createCookie('localStorage', data, 365);
|
||||
}
|
||||
}
|
||||
|
||||
function clearData() {
|
||||
if (type == 'session') {
|
||||
window.top.name = '';
|
||||
} else {
|
||||
createCookie('localStorage', '', 365);
|
||||
}
|
||||
}
|
||||
|
||||
function getData() {
|
||||
var data = type == 'session' ? window.top.name : readCookie('localStorage');
|
||||
return data ? JSON.parse(data) : {};
|
||||
}
|
||||
|
||||
|
||||
// initialise if there's already data
|
||||
var data = getData();
|
||||
|
||||
return {
|
||||
clear: function () {
|
||||
data = {};
|
||||
clearData();
|
||||
},
|
||||
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++;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
removeItem: function (key) {
|
||||
delete data[key];
|
||||
setData(data);
|
||||
},
|
||||
setItem: function (key, value) {
|
||||
data[key] = value+''; // forces the value to a string
|
||||
setData(data);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
if (!window.localStorage) window.localStorage = new Storage('local');
|
||||
if (!window.sessionStorage) window.sessionStorage = new Storage('session');
|
||||
|
||||
})();
|
||||
|
|
@ -7,6 +7,7 @@
|
|||
<!--[if lt IE 8]>
|
||||
<link rel=stylesheet href=../assets/ie7.css>
|
||||
<script src=http://d1eqzjbvoh1rux.cloudfront.net/json2.min.js></script>
|
||||
<script src=../assets/storage-polyfill.js></script>
|
||||
<![endif]-->
|
||||
<style>
|
||||
#gh { text-align: center }
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
<!--[if lt IE 8]>
|
||||
<link rel=stylesheet href=../assets/ie7.css>
|
||||
<script src=http://d1eqzjbvoh1rux.cloudfront.net/json2.min.js></script>
|
||||
<script src=../assets/storage-polyfill.js></script>
|
||||
<![endif]-->
|
||||
<style>
|
||||
#gh { text-align: center }
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
</script>
|
||||
<!--[if lt IE 8]>
|
||||
<script src=http://d1eqzjbvoh1rux.cloudfront.net/json2.min.js></script>
|
||||
<script src=../../assets/storage-polyfill.js></script>
|
||||
<![endif]-->
|
||||
<script src=../../assets/gitter.js></script>
|
||||
<script src=../../assets/store.js></script>
|
||||
|
|
|
|||
Loading…
Reference in a new issue