Web Storage mechanisms Localstorage and Sessionstorage

Source: Internet
Author: User
Tags numeric value javascript array sessionstorage

Https://www.cnblogs.com/yaoyuqian/p/7901052.html

There are two types of Web Storage: Sessionstorage and Localstorage (both are limited to the document source level and cannot be shared between non-homologous documents)

1.sessionstorage data placed on the server (ie not supported) is strictly used to store data in a browser session and the data will be deleted immediately after the browser is closed. Span class= "Hljs-number" >2.localstorage data on the client (low version IE (IE6, IE7) is not supported, and the query language is not supported) store data persistently across sessions localstorage and Sessionsto Rage difference: Localstorage can read/modify to the same Localstorage data as long as the same protocol, the same hostname, and the same port. Sessionstorage is a bit more stringent than localstorage, except for protocols, hostnames, and ports, and requires the same window (that is, the browser's tab). Use the same: (Take sessionstorage as an example) 1. Added: Sessionstorage.setitem (key, value); //value can be any data type 2. Get: Sessionstorage.getitem (key); 3. Delete: Sessionstorage.removeitem (key); //Delete the key value of the storage object in the keys to 4. Clear all: Sessionstorage.clear (); //clears all the key values in the storage object 5. Search (check): Sessionstorage.key ();            
    Localstorage.setitem ("x", 1);//stores a numeric value with the name "x"    localstorage.x = 1;//Adds a key/value pair Localstorage.getitem directly to the Web storage object        (" X ");//Get the value    localstorage.x;//retrieve the key/value pair        Localstorage.removeitem (" x ") directly from the Web storage object;//delete the" X "item.    //removeitem is the only common way to delete a single name-value pair. (because IE8 does not support the delete operator)    localstorage.clear ();//delete all. The only way to delete all name-value pairs in a Stored object        //to store an array as a string    var MyArray = new Array (' First name ', ' Last Name ', ' Email Address ');    Localstorage.formdata = Json.stringify (MyArray);        Retrieve the string version of the array and convert it to an available JavaScript array    var myArray = Json.parse (Localstorage.formdata);
  There is a more practical way to access:   
/span>

var obj = {
Name: ' Doctorhou ',
Describe: ' tall, mighty, handsome '
};
Localstorage.setitem (' Test ', json.stringify (obj));
var getobj = json.parse (Localstorage.getitem (' Test '));

    When the storage data changes (corresponding events): Window.onstorage    

Example:

<body>        

Web Storage mechanisms 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.