问题
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