HTML5 localStorage for offline websites

有些话、适合烂在心里 提交于 2019-12-14 01:24:50

问题


I'm creating a simple app that a user will download onto his/her computer and then open it with a browser off the harddrive. I need it to be able to store data, using any of the standard browsers. (Although data does not have to be accessible between browsers).

As described by this link, If using HTML5's localstorage, Internet Explorer throws an error if the url of the site is something like file:///C:/Web/mylocaltodo.html. It says it can be gotten round by using 'localhost', but the sample application it provides doesn't how how this would be done.

Is it possible to save data locally for an offline site without the user installing a web server on localhost? And have it work on any modern browser, including Internet Explorer?

Thanks


回答1:


Although the question is still old, I was facing a similiar problem when creating an application that should synchronize between multiple windows.

So I'd just like to share my experiences with other developers, that may avoid a long search for the problem.

In fact, due to the same origin policy (as for local file access via XMLHttpRequest), access is denied for reading the localStorage, so that changes won't get synchronized between windows.

In short: If you're using file:/// as URL, it won't be able to synchronize the localStorage. Using a simple webserver and http://localhost/ as URL, it will.

Strange behavior and sadly, there is no error message in the javascript console. Keep that in mind when trying out some localStorage experiments offline and don't get crazy searching for the issue in your code ;-)



来源:https://stackoverflow.com/questions/14096444/html5-localstorage-for-offline-websites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!