JSON common methods and local storage methods

Source: Internet
Author: User
Tags delete key set cookie sessionstorage

1, Json.parse () "Parse JSON string into JSON object"

2, Json.stringify () "Parse JSON object into JSON string"

<script> let    obj = ' {' name ': ' Bob '} ';    Console.log (json.stringify (obj), ' changed to JSON object ') let    obj1 = {name: ' wow '};    Console.log (Json.stringify (obj1), ' changed to JSON string ') </script>

Console results:

JSON is commonly used in these two methods, to deal with the background data;

--------------------------------------------------------------------------------------------------------------- ---

3, Localstorage (local storage)
For long-term preservation of the entire site's data, the saved data has no expiration time until manually removed. ()

4. Sessionstorage (Session storage)
Used to temporarily save data for the same window (or tab), which will be deleted after closing the Window or tab page.

<script>    /*localstorage Syntax */    localstorage.setitem (' name ', ' Bob ')    localstorage.setitem (' Price ',    Console.log (localstorage.length, ' localstorage length ')    console.log (Localstorage.getitem (' name '), ' Get localstorage Set key ')    Console.log (Localstorage.removeitem (' name '), ' remove the key from the Localstorage setting (single) ')    Console.log (Localstorage.clear (), ' Remove key (All) ' from Localstorage set)    /*sessionstorage syntax *    / Sessionstorage.setitem (' age ', a)    sessionstorage.setitem (' price ')    Console.log ( Sessionstorage.length, ' sessionstorage length ')    console.log (Sessionstorage.getitem (' age '), ' Get sessionstorage Set key ')    Console.log (Sessionstorage.removeitem (' age '), ' remove the key from the Sessionstorage setting (single) ')    Console.log (Sessionstorage.clear (), ' Remove Sessionstorage set key (All) ') </script>

Just set it up like this:

Execute corresponding code console and application here correspond to changes;

5.cookie Commentary:

Simply put: A cookie is stored in the form of a key-value pair, the key=value format;
Each cookie is generally ";" Separated
The cookie is temporarily stored by default, and is automatically destroyed when the browser closes the process;

Cookies: Storing data, when a user visits a website (webpage), we can use cookies to store data like a visitor's computer;
1, different browser stored cookie location is not the same, is not universal;
2. The storage of the cookie is differentiated by the way of the domain name;
3, the data of the cookie can be set name, Name=bob
4. The number of cookies stored under a domain name is limited, and the number of different browsers stored is not the same;
5, each cookie storage content size is also limited, different browser storage size limit is not the same;

<script>//Set expiration time for a cookie var godate = new Date (); Godate.setdate (Godate.getdate () + 7);    Set to expire 7 days after the current time Document.cookie = ' name=bob;expires= ' + godate.togmtstring (); Console.log (document.cookie, ' read Cookie ')//name=bob;
Content is best coded to store, encode--encodeuri (), decode--decodeuri () \ n line document.cookie = ' name= ' + encodeuri (' bob\n hello ') + '; expires= ' + godate . togmtstring (); Console.log (decodeURI (Document.cookie)); Name=bob Hello
Package var cookie = {Set:function (key, Val, time) {//Set Cookie method var date = new Date ();//Get current time var expiresdays = time; Set Date to n days after Date.settime (Date.gettime () + expiresdays * 24 * 3600 * 1000); Time Document.cookie = key + "=" + val + "; expires=" + date.togmtstring () formatted as cookie recognition; Set Cookie}, Get:function (key) {//Get Cookie Method */* Get cookie parameter */var GetCookie = Documen T.cookie.replace (/[]/g, ""); Obtain a cookie and format the obtained cookie, remove the space character var cookies = Getcookie.split (";")//The cookie will be obtained with a "semicolon" to identify the cookie to be stored in an array of cookies var Cookiekey; Declare variable Cookiekey for (var i = 0; i < cookies.length; i++) {//use for loop to find the Cookiekey variable var in the cookie arr = cookies[i].split ("="); A single cookie is identified with an "equal sign" and a single cookie is saved as an ARR array if (key = = Arr[0]) {//Match variable name, where arr[0] refers to the cookie name, and if the variable is cookiekey, the execution Assignment operation in a broken statement cookiekey = arr[1]; Assigns the value of a cookie to a variable Cookiekey Break Terminate for Loop Traversal}} return tips; }, Delete:function (key) {//delete Cookie method var date = new Date ();//Get Current time Date.settime (date.get Time ()-10000); Set date to the past time Document.cookie = key + "=V; expires = "+ date.togmtstring (); Set Cookie}}
//CallCookie.set ("Price", 100, 24); Set to 24 days expiration//cookie.delete ("price"); Delete key alert (Cookie.get ("price")); Get cookie</script>

fix;;;

Turn from: https://www.cnblogs.com/lhl66/p/9525571.html;

JSON common methods and local storage methods

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.