HTML5 Learning Notes Client Storage data method: Localstorage (), Sessionstorage ()

Source: Internet
Author: User
Tags sessionstorage

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 following example uses the Localstroage () method to count the number of times a user accesses a page

<script type= "Text/javascript" >

if (Localstorage.pagecount)

{

Localstorage.pagecount=number (Localstorage.pagecount) +1;

}

Else

{

Localstorage.pagecount=1;

}

document.write ("Visit" +localstorage.pagecount+ "times.");

</script>

You can see the number of visits visits:1 times after you run this code. The number of times you refresh the page continues to grow. When the page is closed, it will continue to grow on its original basis.

  

The following example uses the Sessionstorage () method to count the number of times a page is accessed in the current session

<script type= "Text/javascript" >

if (Sessionstorage.pagecount)

{

Sessionstorage.pagecount=number (Sessionstorage.pagecount) +1;

}

Else

{

Sessionstorage.pagecount=1;

}

document.write ("Visit" +sessionstorage.pagecount+ "times.");

</script>

The visits 1 times will be displayed after the code is run. After the page is refreshed, it will continue to grow, but the data will be discarded when the page is closed, and counted back from 1 when it is opened.

HTML5 Learning Notes Client Storage data method: Localstorage (), Sessionstorage ()

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.