Skip to content

Instantly share code, notes, and snippets.

@Juszczak
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Juszczak/81911fe3872a68e9923b to your computer and use it in GitHub Desktop.
Save Juszczak/81911fe3872a68e9923b to your computer and use it in GitHub Desktop.
Don't let localStorage/sessionStorage setItem throw errors in Safari Private Browsing Mode
// Safari, in Private Browsing Mode, looks like it supports localStorage but all calls to setItem
// throw QuotaExceededError. We're going to detect this and just silently drop any calls to setItem
// to avoid the entire page breaking, without having to do a check at each usage of Storage.
(function(){
if (typeof localStorage === 'object') {
try {
localStorage.setItem('localStorage', 1);
localStorage.removeItem('localStorage');
} catch (e) {
Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function() {};
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment