Created
March 31, 2010 14:58
-
Star
(274)
You must be signed in to star a gist -
Fork
(85)
You must be signed in to fork a gist
-
-
Save remy/350433 to your computer and use it in GitHub Desktop.
Revisions
-
remy revised this gist
Feb 1, 2011 . 1 changed file with 7 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ if (typeof window.localStorage == 'undefined' || typeof window.sessionStorage == 'undefined') (function () { var Storage = function (type) { function createCookie(name, value, days) { @@ -59,8 +59,10 @@ var Storage = function (type) { var data = getData(); return { length: 0, clear: function () { data = {}; this.length = 0; clearData(); }, getItem: function (key) { @@ -77,16 +79,18 @@ var Storage = function (type) { }, removeItem: function (key) { delete data[key]; this.length--; setData(data); }, setItem: function (key, value) { data[key] = value+''; // forces the value to a string this.length++; setData(data); } }; }; if (typeof window.localStorage == 'undefined') window.localStorage = new Storage('local'); if (typeof window.sessionStorage == 'undefined') window.sessionStorage = new Storage('session'); })(); -
remy revised this gist
Dec 3, 2010 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -35,22 +35,22 @@ var Storage = function (type) { function setData(data) { data = JSON.stringify(data); if (type == 'session') { window.name = data; } else { createCookie('localStorage', data, 365); } } function clearData() { if (type == 'session') { window.name = ''; } else { createCookie('localStorage', '', 365); } } function getData() { var data = type == 'session' ? window.name : readCookie('localStorage'); return data ? JSON.parse(data) : {}; } -
remy revised this gist
Dec 3, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -64,7 +64,7 @@ var Storage = function (type) { clearData(); }, getItem: function (key) { return data[key] === undefined ? null : data[key]; }, key: function (i) { // not perfect, but works -
remy created this gist
Mar 31, 2010 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -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'); })();