localStorage、sessionStorage、session和cookie的技术优缺点是什么,什么时候使用其中一个而不是另一个?
当前回答
本地存储:保存没有过期日期的用户信息数据,当用户关闭浏览器窗口时,这些数据不会被删除,它将在天,周,月和年可用。
本地存储可以存储5-10mb的离线数据。
//Set the value in a local storage object
localStorage.setItem('name', myName);
//Get the value from storage object
localStorage.getItem('name');
//Delete the value from local storage object
localStorage.removeItem(name);//Delete specifice obeject from local storege
localStorage.clear();//Delete all from local storege
会话存储:它与本地存储日期相同,只是当web用户关闭浏览器窗口时,它会删除所有窗口。
在会话存储可以存储高达5 mb的数据
//set the value to a object in session storege
sessionStorage.myNameInSession = "Krishna";
会话:会话是存储在服务器上的全局变量。每个会话都分配了一个惟一的id,用于检索存储的值。
cookie: cookie是数据,以名称-值对的形式存储在计算机上的小文本文件中。一旦设置了cookie,随后的所有页面请求都会返回cookie名称和值。
其他回答
好吧,LocalStorage,因为它被称为你的浏览器的本地存储,它可以节省高达10MB, SessionStorage也一样,但正如它的名字所说,它是基于会话的,关闭浏览器后将被删除,也可以节省比LocalStorage更少的数据,比如最多5MB,但cookie是非常小的数据存储在你的浏览器中,可以节省4KB,可以通过服务器或浏览器访问…
我还创建了下面的图像来显示差异:
本地存储:保存没有过期日期的用户信息数据,当用户关闭浏览器窗口时,这些数据不会被删除,它将在天,周,月和年可用。
本地存储可以存储5-10mb的离线数据。
//Set the value in a local storage object
localStorage.setItem('name', myName);
//Get the value from storage object
localStorage.getItem('name');
//Delete the value from local storage object
localStorage.removeItem(name);//Delete specifice obeject from local storege
localStorage.clear();//Delete all from local storege
会话存储:它与本地存储日期相同,只是当web用户关闭浏览器窗口时,它会删除所有窗口。
在会话存储可以存储高达5 mb的数据
//set the value to a object in session storege
sessionStorage.myNameInSession = "Krishna";
会话:会话是存储在服务器上的全局变量。每个会话都分配了一个惟一的id,用于检索存储的值。
cookie: cookie是数据,以名称-值对的形式存储在计算机上的小文本文件中。一旦设置了cookie,随后的所有页面请求都会返回cookie名称和值。
LocalStorage Pros: Web storage can be viewed simplistically as an improvement on cookies, providing much greater storage capacity. If you look at the Mozilla source code we can see that 5120KB (5MB which equals 2.5 Million chars on Chrome) is the default storage size for an entire domain. This gives you considerably more space to work with than a typical 4KB cookie. The data is not sent back to the server for every HTTP request (HTML, images, JavaScript, CSS, etc) - reducing the amount of traffic between client and server. The data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site. Cons: It works on same-origin policy. So, data stored will only be available on the same origin. Cookies Pros: Compared to others, there's nothing AFAIK. Cons: The 4K limit is for the entire cookie, including name, value, expiry date etc. To support most browsers, keep the name under 4000 bytes, and the overall cookie size under 4093 bytes. The data is sent back to the server for every HTTP request (HTML, images, JavaScript, CSS, etc) - increasing the amount of traffic between client and server. Typically, the following are allowed: 300 cookies in total 4096 bytes per cookie 20 cookies per domain 81920 bytes per domain(Given 20 cookies of max size 4096 = 81920 bytes.) sessionStorage Pros: It is similar to localStorage. The data is not persistent i.e. data is only available per window (or tab in browsers like Chrome and Firefox). Data is only available during the page session. Changes made are saved and available for the current page, as well as future visits to the site on the same tab/window. Once the tab/window is closed, the data is deleted. Cons: The data is available only inside the window/tab in which it was set. Like localStorage, it works on same-origin policy. So, data stored will only be available on the same origin.
跨标签签出-如何促进跨来源浏览器标签之间的简单通信。
这些是JavaScript中“window”对象的属性,就像document是window对象的属性之一,它包含DOM对象。
Session Storage属性为每个给定的源维护一个单独的存储区域,在页面会话期间可用,即只要浏览器打开,包括页面重新加载和恢复。
本地存储做同样的事情,但即使浏览器关闭并重新打开也会持续存在。
您可以通过以下方式设置和检索已存储数据:
sessionStorage.setItem('key', 'value');
var data = sessionStorage.getItem('key');
localStorage也是如此。
这里是一个快速的回顾和简单而快速的理解
来自freecodecamp的Beau Carnes教练