Skip to content

Instantly share code, notes, and snippets.

@burhanuddin7
Created November 8, 2022 11:02
Show Gist options
  • Save burhanuddin7/4769723b445001ac1dfc0ce862cc6c9f to your computer and use it in GitHub Desktop.
Save burhanuddin7/4769723b445001ac1dfc0ce862cc6c9f to your computer and use it in GitHub Desktop.
LocalStorage common function with try catch
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