Comments: Web Storage in html5 includes two Storage methods: sessionStorage and localStorage. SessionStorage is used to locally store data in a session, this data can be accessed only on pages in the same session. When the session ends, the data is destroyed. Therefore, sessionStorage is not a persistent local storage, but also a session-level storage. LocalStorage is used for persistent local storage. Unless data is actively deleted, the data will never expire.
I. Differences between web storage and cookie
The concept of Web Storage is similar to that of cookie. The difference is that it is designed for larger Storage capacity. The Cookie size is limited, and the Cookie will be sent every time you request a new page. This will result in a waste of bandwidth. In addition, the cookie also needs to specify the scope, cross-origin calls are not allowed.
In addition, Web Storage has methods such as setItem, getItem, removeItem, and clear. Unlike cookies, front-end developers need to encapsulate setCookie and getCookie by themselves.
However, cookies are indispensable: cookies interact with servers and exist as part of HTTP specifications, web Storage is only generated to "Store" data locally (from @ otakustay)
Ii. browser support for html5 web storage
In addition to IE7 and below, browser support is fully supported by other standard browsers (ie and FF must be run on the web server). It is worth mentioning that IE is always a good thing, for example, UserData in IE7 and IE6 is actually a local javascript storage solution. Through simple code encapsulation, all browsers support web storage.
You can use the following code to determine whether the browser supports localStorage:
The Code is as follows: if (window. localStorage ){
Alert ("localStorage supported for browsing ")
}
Else
{
Alert ("localStorage is not supported for browsing ")
}
// Or if (typeof window. localStorage = 'undefined') {alert ("localStorage is not supported for browsing ")}
Iii. localStorage and sessionStorage operations
Both localStorage and sessionStorage have the same operation methods, such as setItem, getItem, and removeItem.
LocalStorage and sessionStorage methods:
SetItem storage value
Purpose: store the value to the key field.
Usage:. setItem (key, value)
Sample Code:
The Code is as follows: sessionStorage. setItem ("key", "value ");
LocalStorage. setItem ("site", "js8.in ");
Get value through getItem
Purpose: Obtain the local storage value of a specified key.
Usage:. getItem (key)
Sample Code:
The Code is as follows: var value = sessionStorage. getItem ("key ");
Var site = localStorage. getItem ("site ");
RemoveItem delete key
Purpose: Delete the local storage value of the specified key.
Usage:. removeItem (key)
Sample Code:
The Code is as follows: sessionStorage. removeItem ("key ");
LocalStorage. removeItem ("site ");
Clear all key/value
Purpose: Clear all keys/values.
Usage:. clear ()
Sample Code:
The Code is as follows: sessionStorage. clear ();
LocalStorage. clear ();
4. Other operations: Click operations and []
Web Storage can not only use its own setItem, getItem, and other convenient access, but also use points (.) like common objects (.) operator, and the [] method for data storage, as shown in the following code:
The Code is as follows:
Var storage = window. localStorage; storage. key1 = "hello ";
Storage ["key2"] = "world ";
Console. log (storage. key1 );
Console. log (storage ["key2"]);
V. key and length attributes of localStorage and sessionStorage are traversed.
The keys () and length provided by sessionStorage and localStorage can be used to conveniently traverse stored data. For example, the following code:
The Code is as follows:
Var storage = window. localStorage;
For (var I = 0, len = storage. length; I <len; I ++)
{
Var key = storage. key (I );
Var value = storage. getItem (key );
Console. log (key + "=" + value );
}
Vi. storage events
Storage also provides storage events. When the key value changes or the value is clear, the storage event can be triggered. The following Code adds a listener for storage event changes:
The Code is as follows: if (window. addEventListener ){
Window. addEventListener ("storage", handle_storage, false );
}
Else if (window. attachEvent)
{
Window. attachEvent ("onstorage", handle_storage );
}
Function handle_storage (e ){
If (! E) {e = window. event ;}
}
The specific attributes of the storage event object are as follows:
Property |
Type |
Description |
Key |
String |
The named key that was added, removed, or moddified |
OldValue |
Any |
The previous value (now overwritten), or null if a new item was added |
NewValue |
Any |
The new value, or null if an item was added |
Url/uri |
String |
The page that called the method that triggered this change |