Created
September 16, 2018 11:41
-
-
Save anurag-majumdar/bed98512793a0e00ed4977ccebc58a26 to your computer and use it in GitHub Desktop.
This file contains 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
if (typeof window.localStorage == 'undefined' || typeof window.sessionStorage == 'undefined') (function () { | |
// Define the storage type (local or session) | |
var Storage = function (type) { | |
// Define the storage type (local or session) | |
function setData(data) { | |
// Sets the data into storage | |
} | |
function clearData() { | |
// clears data from storage | |
} | |
return { | |
length: 0, | |
clear: function () { | |
clearData(); | |
}, | |
getItem: function (key) { | |
return data[key] === undefined ? null : data[key]; | |
}, | |
key: function (i) { | |
var ctr = 0; | |
for (var k in data) { | |
if (ctr == i) return k; | |
else ctr++; | |
} | |
return null; | |
}, | |
removeItem: function (key) { | |
delete data[key]; | |
this.length--; | |
setData(data); | |
}, | |
setItem: function (key, value) { | |
data[key] = value + ''; | |
this.length++; | |
setData(data); | |
} | |
}; | |
}; | |
// Set the local and session storage properties inside the window | |
// object | |
if (typeof window.localStorage == 'undefined') window.localStorage = new Storage('local'); | |
if (typeof window.sessionStorage == 'undefined') window.sessionStorage = new Storage('session'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment