Home>Article> What is the difference between sessionstorage and localstorage?

What is the difference between sessionstorage and localstorage?

青灯夜游
青灯夜游 Original
2020-11-09 14:15:18 33888browse

Difference: The localStorage life cycle is permanent. Unless the user clears the localStorage information, this information will exist forever; the sessionStorage life cycle is the current window or tab. Once the window or tab is permanently closed, then all passing The data it stored will be cleared.

What is the difference between sessionstorage and localstorage?

localStorage and sessionStorage are both objects used to store temporary client information.

They can only store objects of string type (although other native types of objects can be stored in the specification, but so far no browser has implemented it).

The localStorage life cycle is permanent, which means that unless the user clears the localStorage information on the UI provided by the browser, the information will exist forever.

The sessionStorage life cycle is the current window or tab. Once the window or tab is permanently closed, all data stored through sessionStorage will be cleared.

Different browsers cannot share information in localStorage or sessionStorage. Different pages in the same browser can share the same localStorage (the pages belong to the same domain name and port), but sessionStorage information cannot be shared between different pages or tabs. It should be noted here that pages and tabs only refer to top-level windows. If a tab page contains multiple iframe tags and they belong to the same source page, sessionStorage can be shared between them.

Judgment rules for same origin: comparison of

URL"http://www.example.com/dir/page.html".

Same origin Same protocol, host, port Same origin Same protocol, host, port Same origin Same protocol, Host, port different source Same protocol, host, different ports Different Source Different protocols Different sources Different hosts Different sources Different hosts (requires exact match) #http://v2.www.example.com/dir/other.html http://www.example.com:80/dir/other.html Unlike other browsers, IE does not have it when calculating the source Includes ports.
Compare URL Result Result
##http://www.example.com/dir/page2.html
http://www.example.com/dir2/other.html
http://username:password@www.example.com/dir2/other.html
http://www.example.com:81/dir/other.html
https://www.example.com/dir/other.html
http://en.example.com/dir/other.html
http://example.com/dir/other.html
Different sources Different hosts (requires exact match)
Depends on the situation The port is clear and relies on the browser to implement

The parse and stringify provided by the JSON object can convert other data types into strings and then store them in storage.

How to operate:

Save:

var obj = {"name":"xiaoming","age":"16"} localStorage.setItem("userInfo",JSON.stringify(obj));

Get:

var user = JSON.parse(localStorage.getItem("userInfo"))

Delete:

localStorage.remove("userInfo);

Clear:

localStorage.clear();

The above is the detailed content of What is the difference between sessionstorage and localstorage?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn