javascript Internet Explorer 本地存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10487157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:03:45  来源:igfitidea点击:

Internet Explorer Local Storage

javascripthtmlinternet-explorerlocal-storage

提问by ewein

I have a application which works great on all browsers but IE. I am using the local database provided with HTML5. I need to store 3 attributes for the users of the application. I know in IE I can't use the database approach and I was thinking of using 3 arrays stored in local storage, one array for each attribute. Is there a better (and easier) way of doing this?

我有一个应用程序,它适用于除 IE 之外的所有浏览器。我正在使用 HTML5 提供的本地数据库。我需要为应用程序的用户存储 3 个属性。我知道在 IE 中我不能使用数据库方法,我正在考虑使用存储在本地存储中的 3 个数组,每个属性一个数组。有没有更好(更简单)的方法来做到这一点?

Thanks

谢谢

For example, I want to store 1) destination 2) where they are from 3) date So I was thinking I will store an array for destinations, an array fro from locations and an array for dates. Then using some id I can index the arrays and get the corresponding information.

例如,我想存储 1) 目的地 2) 它们来自哪里 3) 日期所以我想我将存储一个目的地数组、一个来自位置的数组和一个日期数组。然后使用一些 id 我可以索引数组并获取相应的信息。

采纳答案by Rob Cooper

If you need local storage, then you need local storage! Based on the information you describe, I think it's probably too heavy to use in a session cookie.

如果您需要本地存储,那么您需要本地存储!根据您描述的信息,我认为在会话 cookie 中使用它可能太重了。

Check out the docs. Be aware that only IE8+ supports this, so if you need to support other versions - you'll need to do some extra work.

查看文档。请注意,只有 IE8+ 支持此功能,因此如果您需要支持其他版本 - 您需要做一些额外的工作。

I would personally enable localStorage where possible, then fall back to a round-trip to the server if the browser doesn't support it (and the data is definitely too much for cookies).

我会在可能的情况下亲自启用 localStorage,然后如果浏览器不支持它,则回退到服务器的往返(并且数据对于 cookie 来说肯定太多了)。

Update RE Polyfills

更新 RE Polyfills

Keep in mind that the polyfills suggested by Rafaelwill fall back to cookies if the browser doesn't support localStorage etc. Be sure to test with a good spread of data, and keep in mind that it will be sending all that data with each request (which has it's own ramifications).

请记住,如果浏览器不支持 localStorage 等,Rafael建议的polyfill将回退到 cookie。请务必使用良好的数据传播进行测试,并记住它将随每个请求发送所有数据(这有它自己的后果)。

回答by Rafael Motta

For IE, you can use a polyfill to simulate a native localStorage: https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills

对于 IE,可以使用 polyfill 模拟原生的 localStorage:https: //github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills

LocalStorage uses a key:value system. But you can save more than one data in one key, using JSON.Stringify & JSON.parse.

LocalStorage 使用键:值系统。但是您可以使用 JSON.Stringify 和 JSON.parse 将多个数据保存在一个键中。

回答by pedromaria

localStorage in IE does not work with this url: file:///P:/Dropbox/abc_web/ingrid8/ingrid.htm#car..

IE 中的 localStorage 不适用于此 url:file:///P:/Dropbox/abc_web/ingrid8/ingrid.htm#car..

through network if it works: file://pedrojelp/p/Dropbox/abc_web/ingrid8/ingrid.htm#car..

通过网络,如果它有效:file://pedrojelp/p/Dropbox/abc_web/ingrid8/ingrid.htm#car..