Skip to content

Commit 960f481

Browse files
committed
Only try to use localStorage when it's present
1 parent 01a97b2 commit 960f481

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

html/js/ejs.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ window.addEventListener("load", () => {
8484
}
8585

8686
const codeId = node.firstChild.id
87-
let code = localStorage.getItem(codeId) || node.textContent
87+
let code = (window.localStorage && localStorage.getItem(codeId)) || node.textContent
8888
let wrap = node.parentNode.insertBefore(elt("div", {"class": "editor-wrap"}), node)
8989
let editor = CodeMirror(div => wrap.insertBefore(div, wrap.firstChild), {
9090
value: code,
@@ -104,7 +104,8 @@ window.addEventListener("load", () => {
104104
clearTimeout(pollingScroll)
105105
pollingScroll = setTimeout(pollScroll, 500)
106106
})
107-
editor.on("change", debounce(() => localStorage.setItem(codeId, editor.getValue()), 250))
107+
if (window.localStorage)
108+
editor.on("change", debounce(() => localStorage.setItem(codeId, editor.getValue()), 250))
108109
wrap.style.marginLeft = wrap.style.marginRight = -Math.min(article.offsetLeft, 100) + "px"
109110
setTimeout(() => editor.refresh(), 600)
110111
if (e) {

0 commit comments

Comments
 (0)