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)
Pseudo-elements and pseudo-classes Speaking of th...
The methods and concepts of private filters and g...
When developing for mobile devices, you often enc...
Benefits of a programmatic approach 1. Global con...
Linux grep command The Linux grep command is used...
I have summarized 3 methods to deploy multiple fr...
<br />Original source: http://www.a-xuan.cn/...
First, download the installation package from the...
Problem Description A Spring + Angular project wi...
Table of contents Preface application Filters Dra...
Background <br />Students who work on the fr...
This article example shares the specific code for...
This article shares the specific code for JavaScr...
Table of contents Simple Factory Factory Method S...
Table of contents Preface Array.isArray construct...