Are cookies fit for just client side usage?

心已入冬 提交于 2019-12-02 00:25:40

问题


I want to store some non confidential data on the user's browser (or any device/mobile browser) for the next session. I can store that on the cookies but I dont want to send that cookie on any requests to server.

I just need to store and retrieve about 5 key-value pairs, client side only. Is cookie a good fit for this purpose or should I consider local storage? Cross-browser support is very important.


回答1:


localStorage is supported pretty well.
AmplifyJS provides non-cookie fallbacks for other browsers, with cookies being the last resort.




回答2:


Cookies will be sent on every request to the server (assuming the host & path match).

If you just want a certain page, you can restrict the cookies to a path - take a look at this question.

But I'd go with local storage first.




回答3:


LocalStorage is the way to go. Just have a look at this great presentation.




回答4:


Cookies are pre-ajax and were originally implemented to always be sent along in server requests. I would go with local storage with cookies as a fallback for older browsers if you're actually supporting them. People still using IE7 and below aren't exactly performance hounds anyway. If the issue is more about costs on your back end, I would suggest polling to get an idea of how many users will likely actually be relying on the cookie fallbacks. The costs may not actually be significant. IE6 usage is pretty much irrelevant and IE7 is marginalized to the point where people are dropping support for that too. But mostly I would fight like a rabid dog to avoid graceful degradation support for browsers that are over five years old. It solves a lot of problems outright and opens a lot of new doors.



来源:https://stackoverflow.com/questions/10413833/are-cookies-fit-for-just-client-side-usage

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