Html5--web (client) data storage

Source: Internet
Author: User
Tags sessionstorage

Store data on the client

HTML5 provides two new ways to store data on the client:

    • localstorage -data storage with no time limit
    • sessionstorage -Data storage for a session

Previously, these were all done by cookies. However, cookies are not suitable for storing large amounts of data because they are passed by each request to the server, which makes the cookie slow and inefficient.

In HTML5, the data is not passed by each server request, but only when the data is used on the request. It makes it possible to store large amounts of data without compromising site performance.

For different sites, the data is stored in different regions, and a Web site can only access its own data.

HTML5 using JavaScript to store and access data

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"; document.write ( localStorage.lastname );</script>

The following example counts the number of times a user visits a page

<script type= "Text/javascript" >if ( localStorage.pagecount )  {  localstorage.pagecount=number ( localStorage.pagecount ) +1;  } else  {  localStorage.pagecount =1;  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"; document.write ( sessionStorage.lastname );</script>

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

<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>

Html5--web (client) data storage

Related Article

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.