HTML5 Local storage localstorage and Sessionstorage

Source: Internet
Author: User
Tags delete key sessionstorage

I used WordPress as a blog, I did a bit like the function. Design this point like the function, in order to prevent visitors constantly praise, have to make a time interval limit, first of all, my design is in the database record IP and the time of the likes, and then a think that also have to maintain the database (in fact, feel that no one to praise also made so strict what), decisively changed to put the data to local preservation
Locally stored data is first thought of as a cookie and HTML5 localstorage.
Cookie compatibility is good, but the amount of data stored is too small, localstorage storage is large, but the compatibility is not very good, after all, IE6 flow still have a lot of shares. Finally, considering that HTML5 is the way to go, and if both are used to ensure compatibility, then it is better to use cookies directly, I chose localstorage. All right, let's finish it and get to the point. HTML5 Local Storage Localstorage, Sessionstorage introduction.
In HTML5, local storage is the property of a window, including localStorage and sessionStorage , the same usage. But the former is always local, the latter is only accompanied by the session, the window once the end is gone. Now I can use the code to verify the difference between the two.

var _d = new Date (). GetTime (); if (window.localstorage) {    localstorage.setitem (_d,_d);    Sessionstorage.setitem (_d,_d);}

Put this code in the script of the page, after running in Chrome, view Localstorage and Sessionstorage, then close the page and open the open page to compare localstorage and sessionstorage changes
You can see that the Localstorage data has been increased, and the Sessionstorage page is always only one data when it is reopened.


Say the difference and then say the operation of the two, because the use of the same is the same, here take Localstorage to Example:

if (window.localstorage) {//Determine if the browser supports Localstorage    localstorage.setitem ("UserName", "coolfish");//Save a data  Key and value     len =localstorage.length;//read the length of the localstorage data    userName =localstorage.getitem ("UserName");// Read key userName data          Localstorage.removeitem ("UserName");//delete key for userName data    localstorage.clear ();// Clear all the data}

Another thing to note is that the data stored locally is string!

HTML5 Local storage localstorage and 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.