admin管理员组文章数量:1418637
I've developed a local HTML5/JavaScript web application and I'm trying to store data in 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.
I see in Developer Tools (F12) -> Application that a localStorage object exists and my data are there.
If I close the browser, when I re-open the webapp (restarting the browser too) I lost everything.
I however see in Developer Tools (F12) -> Application that a localStorage object exists, but it's void.
I'm not using the localStorage.clear()
instruction and I don't remove any item in my js code.
Browser: I'm using Google Chrome Version 67.0.3396.99 (Official Build) (64-bit) (NOT in incognito mode).
Any idea?
I've developed a local HTML5/JavaScript web application and I'm trying to store data in 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.
I see in Developer Tools (F12) -> Application that a localStorage object exists and my data are there.
If I close the browser, when I re-open the webapp (restarting the browser too) I lost everything.
I however see in Developer Tools (F12) -> Application that a localStorage object exists, but it's void.
I'm not using the localStorage.clear()
instruction and I don't remove any item in my js code.
Browser: I'm using Google Chrome Version 67.0.3396.99 (Official Build) (64-bit) (NOT in incognito mode).
Any idea?
Share Improve this question asked Jul 24, 2018 at 12:33 citar88citar88 731 silver badge8 bronze badges 5- 2 You might have security settings on your browser/puter to automatically clear stuff like that. I'd try a different browser/puter, and/or play around with privacy settings. – Carcigenicate Commented Jul 24, 2018 at 12:34
- 1 localStorage is cleared when these conditions are met by the user under settings: Chrome: Setting > Privacy and Security > Clear browsing data • Time range "All time" • "cookies and other site data" is selected” – Sir Catzilla Commented Jul 24, 2018 at 12:37
- 1 @Carcigenicate I tried Firefox but nothing changes. I'm start thinking it is a puter configuration (maybe a policy? I don't know). – citar88 Commented Jul 24, 2018 at 13:43
- 1 @Miaan I've changed Chrome settings to Time range "Last 4 weeks" and removed check from "cookies and other site data". No changes. – citar88 Commented Jul 24, 2018 at 13:43
- Mind showing your saving and retrieving from Local Storage? – Sir Catzilla Commented Jul 24, 2018 at 14:21
3 Answers
Reset to default 3Solved:
In my JS code I was using the window.onbeforeunload = function() {...}
instruction to prevent browser closing without saving data. I've removed this instruction.
Furthermore, I've deselected the flag "Delete browsing history on exit" from Internet Options.
Now I can restore my data from localStorage even after closing browser and restart my webapp (restarting the browser too).
I've not used local storage much but this works for me.
The set_data();
function sets the data in local storage. You fire this when ever you want to save the current page data.
Then when the browser closes and the page reopens, the localStorage.getItem
finds any stored data.
var data = localStorage.getItem('my_data');
// for testing
alert(data);
set_data = function() {
data = localStorage.setItem('my_data', 'whatever');
// for testing
alert(localStorage.getItem('my_data'));
}
delete_data = function() {
data = localStorage.clear();
// for testing
alert(localStorage.getItem('my_data'));
}
See working jsfiddle https://jsfiddle/joshmoto/j2y8o6rq/
I encountered the same problem, I'm also developing my personal web app, I fixed it with this code, I hope it helps.
let oldStorage = localStorage.getItem('myStorage') || '';
let newStorage = [...oldStorage];
console.log(newStorage);
本文标签: javascriptlocalStorage resets after close browserStack Overflow
版权声明:本文标题:javascript - localStorage resets after close browser - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745296695a2652120.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论