Created
November 8, 2022 11:02
-
-
Save burhanuddin7/4769723b445001ac1dfc0ce862cc6c9f to your computer and use it in GitHub Desktop.
LocalStorage common function with try catch
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
var LocalStorage = (function() { | |
var isSupported = false; | |
try { | |
isSupported = window.localStorage ? true : false; | |
} | |
catch (error) {} | |
return { | |
isSupported: isSupported, | |
set: function(key, val) { | |
if(isSupported) { | |
localStorage.setItem(key, val); | |
} | |
}, | |
get: function(key) { | |
if(isSupported) { | |
return localStorage.getItem(key); | |
} | |
}, | |
remove: function(key) { | |
if(isSupported) { | |
return localStorage.removeItem(key); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment