About HTML5 entry to mastery: HTML5 Web Storage

Source: Internet
Author: User
Keywords Html5 web storage Getting started to master

http://www.aliyun.com/zixun/aggregation/17326.html on Client "> Store data

HTML 5 provides two new ways to store data on the client side:

Localstorage-No time limit data storage

Sessionstorage-Data storage for a session

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

In HTML 5, data is not delivered by each server request, but only when requested. It makes it possible to store large amounts of data without affecting the performance of the Web site.

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

HTML 5 uses JavaScript to store and access data.

Localstorage method

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

How to create and access localstorage:

Instance

<script type= "Text/javascript" >
Localstorage.lastname= "Smith";
document.write (Localstorage.lastname);
</script>

Try it yourself, the code is as follows:

<! DOCTYPE html>
<html>
<body>
<script type= "Text/javascript" >
Localstorage.lastname= "Smith";
document.write ("Last Name:" + localstorage.lastname);
</script>
</body>
</html>

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>

Try it yourself, the code is as follows:

<! DOCTYPE html>
<html>
<body>
<script type= "Text/javascript" >
Localstorage.lastname= "Smith";
document.write ("Last Name:" + localstorage.lastname);
</script>
</body>
</html>

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.