Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mkoryak/d12e965d739fa42462d7 to your computer and use it in GitHub Desktop.
Save mkoryak/d12e965d739fa42462d7 to your computer and use it in GitHub Desktop.
// 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.
if (typeof localStorage === 'object') {
try {
localStorage.setItem('localStorage', 1);
localStorage.removeItem('localStorage');
} catch (e) {
Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function() {};
alert('Your web browser does not support storing settings locally. In Safari, the most common cause of this is using "Private Browsing Mode". Some settings may not save or some features may not work properly for you.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment