HTML5 Sessionstorage and Localstorage use detailed

Source: Internet
Author: User
Tags delete key sessionstorage

The Web Storage in HTML5 includes two ways of storage: Sessionstorage and Localstorage.


Browser support situation:

Browser Support In addition to IE7 and the following are not supported, other standard browsers are fully supported (IE and FF need to run in the Web server), it is worth mentioning that IE always do good, such as IE7, IE6 UserData is actually JavaScript local storage solution. With simple code encapsulation you can unify to all browsers that support Web storage.
to determine whether the browser supports Localstorage, you 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")}

both Localstorage and Sessionstorage have the same methods of operation, such as SetItem, GetItem, and RemoveItem.
usage: . SetItem (key, value)
code Example:
Sessionstorage.setitem ("Key", "value"); Localstorage.setitem ("Site", "js8.in");


GetItem Get value Purpose: Gets the values that are stored locally by the specified key
Usage :. GetItem (Key)

code Example:
var value = Sessionstorage.getitem ("key"); var site = Localstorage.getitem ("site");

RemoveItem Delete key purpose: Delete the value stored locally by the specified key
Usage :. RemoveItem (Key)

code Example:
Sessionstorage.removeitem ("key"); Localstorage.removeitem ("site");

Clear Clears all Key/value uses: Clears all Key/value
Usage :. Clear ()

code Example:
sessionstorage.clear (); Localstorage.clear ();
 usage Examples:

<meta http-equiv= "Content-type" content= "text/html; charset=utf-8" />
<input type= ' button ' onclick= ' Set_item () ' value= ' stored value '/>
<input type= ' button ' onclick= ' get_item () ' value= ' value '/>
<input type= ' button ' onclick= ' Delete_item () ' value= ' delete '/>
<script>
//localstorage stored value is permanently valid
function Set_item () {
var user = {};
user.name = ' Adam Li ';
user.age = +;
user.home = ' China ';
localstorage.setitem (' UserInfo ', json.stringify (user));
}
//localstorage Value
function get_item () {
var data = Json.parse (Localstorage.getitem (' userinfo '));
alert ("Name:" +data.name+ "\ R Age:" +data.age+ "\ R home:" +data.home);
}
//localstorage Delete the value corresponding to the specified key
function Delete_item () {
localstorage.removeitem (' userinfo ');
Alert (Localstorage.getitem (' userinfo '));
}
</script>    

/*                                                                                                                                                                                                   

Sessionstorage use of the method, please refer to the Localstorage use example, the actual application to use which key to see your needs, if it is to be permanently saved, then please use the Localstorage method access value, if you want to browse close the end of the session to clear the cache, Of course you have to choose the Sessionstorage method.

*/       

HTML5 Sessionstorage and Localstorage use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.