HTML5 provides two new ways to store data on the client:
- Localstorage-Data storage with no time limit
- Sessionstorage-Data storage for a session
the data stored by the Localstorage method has no time limit . Data is still available after the second, second, or next year.
The difference between Web storage and cookies
The concept of WEB storage is similar to a cookie, except that it is designed for larger capacity storage. The size of the cookie is limited, and every time you request a new page, the cookie is sent past, which virtually wastes bandwidth, and the cookie needs to specify the scope and cannot be called across domains.
In addition, WEB storage has methods such as setitem,getitem,removeitem,clear, unlike cookies that require front-end developers to encapsulate Setcookie,getcookie themselves.
However, cookies are also not available or missing: The role of cookies is to interact with the server as part of the HTTP specification, and the Web storage only for local "storage" of data
First, to determine whether the browser supports localstorage can use the following code:
if (window.localstorage) {alert ("Browse support Localstorage")}else{ alert ("Browse temporarily does not support Localstorage")}//or if (typeof Window.localstorage = = ' undefined ') {alert ("Browse temporarily does not support Localstorage")}
Second, the Code demo
<! DOCTYPE html>localstorage.setitem (' Keypass ', strpass);Alert ("Remember Password"); }else{localstorage.removeitem (' Keypass ');Alert ("Remember Password"); } window.location.reload (); }</script>Three. Basic usage of Localstorage:SetItem Store value uses: Store value in key field usage:. SetItem (key, value) code example: Localstorage.setitem ("Site", "js8.in" = Localstorage.getitem ("site"); RemoveItem Delete key purpose: Delete the value of the specified key local store usage:. RemoveItem (Key) code example: Localstorage.removeitem ("Site"); clear clears all key/value uses: Clears all key/value usages:. Clear ( ) Code example: Localstorage.clear ();
Original address:
Ultra-Simple Localstorage realizes the function of remembering the password
Other information:
HTML5 storage Methods Sessionstorage and Localstorage detailed
JavaScript remembers user name and login password (two ways) cookie
HTML5 localstorage Implementation remember the password