HTML5 Web Storage

Source: Internet
Author: User

HTML5 Web Storage, a better local storage method than a cookie.

What is HTML5 Web storage?

Use HTML5 to store the user's browsing data locally.

Earlier, local storage was using cookies. But Web storage needs to be more secure and fast. This data is not saved on the server, but it is used only for user requests for Web site data. It can also store large amounts of data without compromising the performance of the site.

Data exists as a key/value pair, and the Web page's data is only allowed to be used by that page.

Browser support

Internet Explorer 8+, Firefox, Opera, Chrome, and Safari support Web Storage.

Note: Internet Explorer 7 and earlier versions of IE do not support Web storage.

Localstorage and Sessionstorage

There is the new objects for storing data on the client:

    • Localstorage-Data storage with no time limit
    • Sessionstorage-Data storage for a session

Before using Web Storage, you should check whether your browser supports localstorage and sessionstorage:

if (typeof (Storage)!== "undefined") {  //yes! localstorage and Sessionstorage support!  Some code ...} else{  //sorry! No Web Storage Support:}
Localstorage Object

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

<Script>functionClickCounter () {if(typeof(Storage)!=="undefined")  {  if(localstorage.clickcount) {Localstorage.clickcount=Number (Localstorage.clickcount)+1; }  Else{Localstorage.clickcount=1; } document.getElementById ("result"). InnerHTML="You have clicked the button" +Localstorage.clickcount+ "Time (s)."; }Else{document.getElementById ("result"). InnerHTML="Sorry, your browser does not support Web Storage ..."; }}</Script>

Sessionstorage Object

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:

<Script>functionClickCounter () {if(typeof(Storage)!=="undefined")  {  if(sessionstorage.clickcount) {Sessionstorage.clickcount=Number (Sessionstorage.clickcount)+1; }  Else{Sessionstorage.clickcount=1; } document.getElementById ("result"). InnerHTML="You have clicked the button" +Sessionstorage.clickcount+ "Time (s) in the this session."; }Else{document.getElementById ("result"). InnerHTML="Sorry, your browser does not support Web Storage ..."; }}</Script>

HTML5 Web Storage

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.