Skip to content

Commit 26391ef

Browse files
author
Wes Cossick
committed
Better localStorage detection
1 parent d77d4b3 commit 26391ef

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/js/simplemde.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,8 +1510,24 @@ SimpleMDE.prototype.render = function(el) {
15101510
this._rendered = this.element;
15111511
};
15121512

1513+
// 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.
1514+
function isLocalStorageAvailable() {
1515+
if(typeof localStorage === "object") {
1516+
try {
1517+
localStorage.setItem("smde_localStorage", 1);
1518+
localStorage.removeItem("smde_localStorage");
1519+
} catch (e) {
1520+
return false;
1521+
}
1522+
} else {
1523+
return false;
1524+
}
1525+
1526+
return true;
1527+
}
1528+
15131529
SimpleMDE.prototype.autosave = function() {
1514-
if(localStorage) {
1530+
if(isLocalStorageAvailable()) {
15151531
var simplemde = this;
15161532

15171533
if(this.options.autosave.uniqueId == undefined || this.options.autosave.uniqueId == "") {
@@ -1564,7 +1580,7 @@ SimpleMDE.prototype.autosave = function() {
15641580
};
15651581

15661582
SimpleMDE.prototype.clearAutosavedValue = function() {
1567-
if(localStorage) {
1583+
if(isLocalStorageAvailable()) {
15681584
if(this.options.autosave == undefined || this.options.autosave.uniqueId == undefined || this.options.autosave.uniqueId == "") {
15691585
console.log("SimpleMDE: You must set a uniqueId to clear the autosave value");
15701586
return;

0 commit comments

Comments
 (0)