1.Json stringJson is mainly used for front-end and back-end interaction. It is a data format that is more convenient to use than Xml. 1.1Json SyntaxCan be used to represent: objects, arrays, simple data types, etc.
Conversion between Json and objects: JSON string to object `JSON.parse(JSON string) will return the converted js object` Object to JSON string `JSON.stringify() is used to convert a value to a JSON string` 1.2 Examples//Convert "string" data in object form to JSON object let s = `{"name":"onion","age":18}`; console.log(s) // string => {"name":"Onion","age":18} console.log(JSON.parse(s)); // //Object: object //Convert "string" data in array form to JSON object let s = `[1,5,8,9]`; console.log(s); // string => [1,5,8,9] console.log(JSON.parse(s)); //Object: object ----------------------------------------------------------------------- //Convert object to json string let s = {"name":"onion","age":18}; console.log(JSON.stringify(s)); // string => {"name":"onion","age":18} // array to json string let s = [1,5,8,9]; console.log(JSON.stringify(s)); // string => [1,5,8,9]
2. Cookies 2.1 How to use it?
Take a look at the example: document.cookie = "name=onion"; document.cookie = "age=18"; The results are as follows: We found the onions too spicy and I thought I'd try potatoes instead: **document.cookie = "name=Onion"; document.cookie = "name=土豆"; document.cookie = "age=18"; The results are as follows: After eating potatoes for a while, I found that potatoes are not good and I don’t want them anymore. What should I do? So how do we delete it? In fact, careful friends have discovered that there is a session level where we can set a validity period, and this date is the expiration date, using the document.cookie = "name=土豆;expires="+new Date('2021/11/25 03:58:20'); 3. Local storage H5 adds 3.1 Basic Use Use window.localstorage to operate localstorage (window can be omitted) //Add setItem localStorage.setItem("name","onion"); //GetItem localStorage.getItem("name","onion"); //deleteremoveItem("key-value pair") localStorage.removeItem("name"); // Clear localStorage.clear(); 3.2 Example (Remember username and password)
Username: <input type="text" id="username"> <br> Password: <input type="password" id="pwd"> <br> <span style="font-size: 14px;">Remember username and password:</span> <input type="checkbox" id="remember"> // Checkbox const remember = document.getElementById('remember'); //Username const username = document.getElementById('username'); //Password const pwd = document.getElementById('pwd'); remember.onclick = function(){ if (remember.checked) { //Select and store the username and password in local storage. localStorage.setItem("username",username.value); localStorage.setItem("pwd",pwd.value); } else { // Change from selected to unselected, delete the username and password from local storage localStorage.removeItem("username"); localStorage.removeItem("pwd"); } console.log(); } //Each time you reopen the page, check if there is a value in the local storage if (localStorage.getItem("username")) { //If there is a value, write the value into the input box. username.value = localStorage.getItem("username") pwd.value = localStorage.getItem("pwd"); //The checkbox is selected by default remember.checked = true; }
This is the end of this article about Json string + Cookie + localstorage in JS. For more relevant Json string + Cookie + localstorage content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Play mp3 or flash player code on the web page
>>: Detailed explanation of the use of css-vars-ponyfill in IE environment (nextjs build)
Table of contents General upload component develo...
Table of contents Create a layout Add CSS styles ...
In the previous article, we introduced the MySQL ...
This article shares the specific code of JavaScri...
Table of contents Pull the image Run the image (g...
Layout part: <div id="slider"> &l...
1. Development environment vue+vant 2. Computer s...
Preface A Docker image consists of a Dockerfile a...
1. Resume nacos database Database name nacos_conf...
Today I accidentally saw the parameter slave_exec...
This article shares the specific code of js to im...
This article will introduce how to use radial-gra...
<br />Original text: http://andymao.com/andy...
The installation method of MySQL5.7 rpm under Lin...
The ps command in Linux is the abbreviation of Pr...