[Localstorage and Sessionstorage in 0]-javascript

Source: Internet
Author: User
Tags sessionstorage

L Content Directory

Brief introduction

Use of storages

Storages VS Cookies

At last

L Introduction

The code in this article is recommended to be executed under Chrome/360chrome.

HTML5 adds Localstorage and Sessionstorage, both of which are objects of window and can be window.localstorage/ Window.sessionstorage access, which is used to save data, is only slightly different from the scope of the save.

The following storage represent Window.localstorage and window.sessionstorage.

L Use of storages

1) Similarities and differences

Stores data as key-value pairs, with the key and value of the data being of type string

Localstorage stored locally, data will not expire unless manually cleared

Sessionstorage accompanies the session, the data stored in Sessionstorage will disappear after the window is closed

2) Inheritance Relationship

Localstorage and Sessionstorage are examples of storage.

    

Using Console.dir (localstorage) to list the data stored in Localstorage, we found a property called __proto__.

object that __proto__ Pointer to the constructor that generated the object. prototype property.

We also know by looking for localstorage.__proto__ that Localstorage is an example of storage.

    actually JavaScript of the instanceof the operator is the way to determine whether an object is an instance of a class.

We can see the methods defined in storage:

Clear () Clears all stored data

GetItem (string key) gets the value corresponding to the key, and the value returned is of type String

Key (number| String index) Gets the key of the specified index data, if index cannot be converted to a number returns the return value of key (0) and returns NULL if no data is stored for index specified indexes, the error will be thrown if no parameter is passed

RemoveItem (String key) removes key-value pairs specified by key

SetItem (string key, String value) sets the key and value, and the argument is converted to a string

3) Business Scenario

Localstorage is suitable for data that is not often modified as a cache store, such as an organization tree, and cached data can be viewed through the Resources tab of Chrome's developer tools (we can see that you can also view Web SQL and cookies, etc. The world is so beautiful.

Sessionstorage stores temporary data that disappears as the window is closed

L storages VS Cookies

The difference between storages and cookies:

1) data stored in storages will not be submitted to the server as requested

2) The maximum cookie is 4KB, and storages current situation can be considered according to 5MB

3) For cookies We do not have a friendly read/write mode using native JavaScript, and for storages we can easily read and write using the storage defined method.

L finally

The first time to write such a blog, lack of experience. If there is a mistake, please correct it in time. Thank you very much.

[Localstorage and Sessionstorage in 0]-javascript

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.