From 26391efa26f6727ce10706e87897d33a753043d4 Mon Sep 17 00:00:00 2001 From: Wes Cossick Date: Mon, 14 Mar 2016 20:59:16 -0500 Subject: [PATCH] Better localStorage detection --- src/js/simplemde.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/js/simplemde.js b/src/js/simplemde.js index e277dbf..73e2089 100644 --- a/src/js/simplemde.js +++ b/src/js/simplemde.js @@ -1510,8 +1510,24 @@ SimpleMDE.prototype.render = function(el) { this._rendered = this.element; }; +// Safari, in Private Browsing Mode, looks like it supports localStorage but all calls to setItem throw QuotaExceededError. We're going to detect this and set a variable accordingly. +function isLocalStorageAvailable() { + if(typeof localStorage === "object") { + try { + localStorage.setItem("smde_localStorage", 1); + localStorage.removeItem("smde_localStorage"); + } catch (e) { + return false; + } + } else { + return false; + } + + return true; +} + SimpleMDE.prototype.autosave = function() { - if(localStorage) { + if(isLocalStorageAvailable()) { var simplemde = this; if(this.options.autosave.uniqueId == undefined || this.options.autosave.uniqueId == "") { @@ -1564,7 +1580,7 @@ SimpleMDE.prototype.autosave = function() { }; SimpleMDE.prototype.clearAutosavedValue = function() { - if(localStorage) { + if(isLocalStorageAvailable()) { if(this.options.autosave == undefined || this.options.autosave.uniqueId == undefined || this.options.autosave.uniqueId == "") { console.log("SimpleMDE: You must set a uniqueId to clear the autosave value"); return;