问题描述
我已经开发了本地 HTML5 / JavaScript Web应用程序,并且试图将数据存储在 localStorage 中.
I've developed a local HTML5/JavaScript web application and I'm trying to store data in localStorage.
如果不关闭浏览器就关闭webapp 选项卡,没有任何问题:当我在新选项卡中重新打开webapp时,可以在localStorage中找到数据.
There are no problems if I close the webapp tab without closing the browser: when I re-open the webapp in a new tab I'm able to find my data in localStorage.
我在开发人员工具(F12)->应用程序中看到一个localStorage对象存在并且我的数据在那里.
I see in Developer Tools (F12) -> Application that a localStorage object exists and my data are there.
如果我关闭浏览器,那么当我重新打开Web应用程序(也重新启动浏览器)时,我将失去一切.
If I close the browser, when I re-open the webapp (restarting the browser too) I lost everything.
但是我在开发人员工具(F12)->应用程序中看到localStorage对象存在,但是它是无效的.
I however see in Developer Tools (F12) -> Application that a localStorage object exists, but it's void.
我没有使用localStorage.clear()
指令,也没有删除我的js代码中的任何项目.
I'm not using the localStorage.clear()
instruction and I don't remove any item in my js code.
浏览器:我使用的是Google Chrome版本67.0.3396.99(正式版本)(64位)(在隐身模式下不可用).
Browser: I'm using Google Chrome Version 67.0.3396.99 (Official Build) (64-bit) (NOT in incognito mode).
有什么主意吗?
推荐答案
已解决:
在我的JS代码中,我正在使用window.onbeforeunload = function() {...}
指令来防止浏览器在不保存数据的情况下关闭.我已经删除了此说明.
In my JS code I was using the window.onbeforeunload = function() {...}
instruction to prevent browser closing without saving data. I've removed this instruction.
此外,我已经从Internet选项中取消选择了退出时删除浏览历史记录"标记.
Furthermore, I've deselected the flag "Delete browsing history on exit" from Internet Options.
现在,即使关闭浏览器并重新启动我的Web应用程序(也可以重新启动浏览器),我仍可以从localStorage还原数据.
Now I can restore my data from localStorage even after closing browser and restart my webapp (restarting the browser too).
这篇关于关闭浏览器后,localStorage重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!