客户端数据本地存储
我想在客户端存储一些数据。 Cookie 是我的第一倾向,但它们会随每个请求一起发送,对吧?有没有一种方法可以在不传输数据的情况下存储数据?我不一定想为每个请求增加 10-20k 的开销。 HTML 5 网络存储是唯一的替代方案吗?有多少浏览器采用了它?
I want to store some data client side. Cookies are my first inclination, but they get sent with every request, right? Is there a way to store data without it being transferred? I don't necessarily want to add 10-20k of overhead for every request. Is the only alternative HTML 5 webstorage and how many browsers have adopted that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
html5 存储已广泛部署,
您可以了解更多信息 @ http://diveintohtml5.ep.io/storage.html< /a>
html5 storage is widely deployed
you can find out more @ http://diveintohtml5.ep.io/storage.html
不,并非所有 cookie 都会随每个请求一起发送。您可以检查 cookie 是否存在,如果不存在则创建它,如果存在则读取它。对于少量数据来说,Cookie 仍然是一个很好的跨浏览器选项。
No, not all cookies get sent with every request. You can check to see if a cookie exists, if not create it, and if so, read it. Cookies are still a good cross-browser option for small amounts of data.
http://fsojs.com 支持强大的文件存储客户端,但目前仅适用于 Chrome
http://fsojs.com supports robust file storage client-side, but only works with Chrome at the moment
正如您所提到的,Cookie 是一种选项,HTML5 规范中的 Web 存储也是一种选项。还可以使用 Flash 来存储数据,其额外好处是该数据可以在同一台计算机上的多个浏览器中保留,但缺点是您需要为没有 Flash 的用户提供后备方案。
就个人而言,将数据保存在服务器上(由会话 ID 或 cookie 标识)将是我的做法,您可以控制数据,并且不必担心当用户清除缓存或切换机器时丢失数据/设备。它也是最容错的,因为它不依赖于浏览器功能和/或插件(也许除了 cookie)。
另一件事,如果您正在寻找使用上述所有内容(cookie、闪存、网络存储)的客户端数据存储的抽象,请查看 Evercookie
As you have mentioned, cookies are an options and so is web storage in the HTML5 spec. There's also the ability to use Flash to store data with the added benefit that this data persists across multiple browsers on the same machine, but the drawback that you'll need a fallback for users who don't have Flash.
Personally, keeping the data on the server (identified by the session id or cookie) would be my way to do it, you have control of the data and don't have to worry about losing it when the user clears their cache or switches machines/devices. It's also the most fault-tolerant because it doesn't rely on browser features and/or plugins (other than perhaps cookies).
One more thing, if you're looking for an abstraction of client-side data storage that uses all of the above (cookies, flash, web storage) check out Evercookie