谈谈本地存储利弊Cookie、localStorage、sessionStorage_html/css_WEB
谈谈Cookie的弊端You talk about the shortcomings of Cookie cookie虽然在持久保存客户端数据提供了方便,分担了服务器存储的负担,但还是有很多局限性的。 第一:每个特定的域名下最多生成20个 cookie
IE和 Opera会清理近期最少使用的 cookie, Firefox会随机清理 cookie。 cookie的最大大约为 4096字节,为了兼容性,一般不能超过 4095字节。 IE 提供了一种存储可以持久化用户数据,叫做 userdata,从 IE5.0就开始支持。每个数据最多128K,每个域名下最多1M。这个持久化数据放在缓存中,如果缓存没有清理,那么会一直存在。 IE can provide a way to store persistent user data, called UserData , from IE5.0 has supported. Each data up to 128K, under each domain up to 1M. The persistent data in the cache, if not cleaned the cache, it will always exist. 优点【Advantages】极高的扩展性和可用性【high scalability and availability】
缺点【Disadvantages】
浏览器本地存储Browser local storage 在较高版本的浏览器中, js提供了 sessionStorage和 globalStorage。在 HTML5中提供了 localStorage来取代 globalStorage。 In later versions of the browser, JS provides sessionStorage and globalStorage . In HTML5 provides in localStorage to replace globalStorage. html5中的 Web Storage包括了两种存储方式: sessionStorage和 localStorage。 html5 The Web Storage includes two storage methods: sessionStorage and localStorage . sessionStorage用于本地存储一个会话【session】中的数据,这些数据只有在同一个会话中的页面才能访问并且当会话结束后数据也随之销毁。因此 sessionStorage不是一种持久化的本地存储,仅仅是会话级别的存储。 sessionStorage data stored locally for a session (session) in which the data only in the same session of the page to access data, and when the end of the session also will be destroyed. So sessionStorage not a persistent local storage, just session-level storage. 而 localStorage用于持久化的本地存储,除非主动删除数据,否则数据是永远不会过期的。 And localStorage for persistent local storage, unless the initiative to remove the data, or the data is never out of date. web storage和cookie的区别the difference between web storage and a cookie Web Storage的概念和 cookie相似,区别是它是为了更大容量存储设计的。 Cookie的大小是受限的,并且每次你请求一个新的页面的时候 Cookie都会被发送过去,这样无形中浪费了带宽,另外 cookie还需要指定作用域,不可以跨域调用。 Web Storage concept and cookie similar, except that it is designed for higher-capacity storage. Cookie size is limited, and every time you request a new page when the Cookie will be sent in the past, so that virtually waste bandwidth, another cookie also need to specify the scope, not cross-domain calls. 除此之外, Web Storage拥有 setItem,getItem,removeItem,clear等方法,不像 cookie需要前端开发者自己封装 setCookie,getCookie。 In addition, Web Storage has setItem, getItem, removeItem, clear methods, unlike the cookie front-end developers need their own package setCookie, getCookie. 但是 cookie也是不可以或缺的: cookie的作用是与服务器进行交互,作为 HTTP规范的一部分而存在 ,而 Web Storage仅仅是为了在本地“存储”数据而生 But the cookie is not indispensable: cookie 's role is to interact with the server as HTTP part of the specification exists, and Web Storage simply to the local "store" raw data 浏览器的支持除了 IE7及以下不支持外,其他标准浏览器都完全支持(ie及FF需在web服务器里运行),值得一提的是IE总是办好事,例如IE7、IE6中的 userData其实就是 javascript本地存储的解决方案。通过简单的代码封装可以统一到所有的浏览器都支持 web storage。 Browser support in addition to IE7 and below are not supported, other standard browsers fully support (ie and FF need to run a web server in), it is worth mentioning that IE always good things, such as IE7, IE6 in the userData fact is javascript local storage solutions. By simple code package can be unified to all the browsers support Web storage. localStorage和 sessionStorage都具有相同的操作方法,例如 setItem、getItem和 removeItem等 localStorage and sessionStorage have the same method of operation, for example setItem, getItem and removeItem etc. cookie 和session 的区别:cookie and session distinction:
|