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.
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"
.
Compare URL | Result | Result |
---|---|---|
##http://www.example.com/dir/page2.html
| Same origin
Same protocol, host, port | |
http://www.example.com/dir2/other.html
| Same origin
Same protocol, host, port | |
http://username:password@www.example.com/dir2/other.html
| Same origin
Same protocol, Host, port | |
http://www.example.com:81/dir/other.html
| different source
Same protocol, host, different ports | |
https://www.example.com/dir/other.html
| Different Source
Different protocols | |
http://en.example.com/dir/other.html
| Different sources
Different hosts | |
http://example.com/dir/other.html
| Different sources
Different hosts (requires exact match) | |
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.
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!