Summary of Localstorage and sessionstorage usage

Source: Internet
Author: User
Tags sessionstorage

Localstorage and the Sessionstorage are the same objects that are used to store client temporary information.

· They can only store objects of the string type (although other primitive types of objects can be stored in the specification, but so far no browser has implemented them).

· Localstorage The life cycle is permanent, which means that unless the user shows up in a browser-provided UI Clear on Localstorage information, otherwise this information will always be present.

sessionstorage The life cycle is the current window or tab, and once the window or tab has been permanently closed, all through sessionstorage

•   different browsers cannot share localstorage sessionstorage (page belongs to the same domain name and port), but cannot be shared between pages or tabs sessionstorage iframe sessionstorage

· Localstorage and the Sessionstorage Use the same API :

Localstorage.setitem ("Key", "value");// in "Key" Store a value for the name "Value"

Localstorage.getitem ("key");// gets the name of "Key" the value

Enumeration Localstorage method of:

for (vari=0;i<localstorage.length;i++) {

var name = Localstorage.key (i);

var value = Localstorage.getitem (name);

}

Delete Localstorage How to store information in:

Localstorage.removeitem ("key");// Remove the name "Key" the information.

localstorage.clear ();// Clear Localstorage All information in

· The information obtained through getItem or directly using localstorage["Key" is a copy of the actual store.

For example:

Localstorage.key ={value1: "Value1"};

Localstorage.key.value1= ' a ';

This is not a function of the actual stored values, and the following wording is not possible:

Localstorage.getitem ("Key"). Value1= "a";

Localstorage method

The data stored by the Localstorage method has no time limit. Data is still available after the second, second, or next year.

How to create and access localstorage:

Instance
<script type= "Text/javascript" >localstorage.lastname= "Smith";d ocument.write (localstorage.lastname); </ Script>

The following example counts the number of times a user accesses a page:

Instance
<script type= "Text/javascript" >if (localstorage.pagecount) {Localstorage.pagecount=number (  Localstorage.pagecount) +1;  }else {localstorage.pagecount=1; }document.write ("Visits" + Localstorage.pagecount + "time (s)"); </script>
Sessionstorage method

The Sessionstorage method stores data for a session. When the user closes the browser window, the data is deleted.

How to create and access a sessionstorage:

Instance
<script type= "Text/javascript" >sessionstorage.lastname= "Smith";d ocument.write (Sessionstorage.lastname); </script>

The following example counts the number of times a user accesses a page in the current session:

Instance
<script type= "Text/javascript" >if (sessionstorage.pagecount) {Sessionstorage.pagecount=number (  Sessionstorage.pagecount) +1;  }else {sessionstorage.pagecount=1; }document.write ("Visits" +sessionstorage.pagecount+ "time (s) this session."); </script>

Reference blog:

Http://www.cnblogs.com/zjcn/archive/2012/07/03/2575026.html

http://adamed.iteye.com/blog/1698740

Http://blog.sina.com.cn/s/blog_6d01cce30101jrdw.html

Http://www.2cto.com/kf/201206/134313.html

Http://www.w3school.com.cn/html5/html_5_webstorage.asp


This article is from the "7439523" blog, please be sure to keep this source http://7449523.blog.51cto.com/7439523/1615532

Summary of Localstorage and sessionstorage usage

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.