Use and encapsulation of cookie s

The concept of cookie s:
Refers to data (usually encrypted) stored on a user's local terminal by certain websites for session tracking in order to identify a user.Exists as text.
Session tracking technology cookie s:
Track each connection, record the information it generates, and send the next one with you
1. Characters, text only
2. Do not allow cross-domain (who saves, who uses)
3. Timeliness, default session level, close browser, delete (valid for a certain period of time, immediate validity)
4. Quantity (around 50) and size (4k) limits
Use of cookie s:
1. Set the default cookie:

document.cookie = "user=admin";
		document.cookie = "pass=123";

When a cookie is retrieved, the document.cookie retrieves all the cookies at once, and the results returned by the browser are presented in the following format
user=admin; pass=123
However, this result does not affect the formatting, how it should be set or how it should be set when it is set.
Do not allow multiple strips to be stored at one time

document.cookie = "a=10;b=20;c=30"; //Only the first will take effect

2. Set cookie s with expiration date

var d = new Date();
		d.setDate(d.getDate() + 3);//Get the specified date
		document.cookie = "abc=123;expires=" + d;
		document.cookie = "qwe=345;path=/";

Be careful:
When a cookie is queried, the validity period and path of the current cookie cannot be obtained
Details of cookie s can only be viewed through tools (browser console)
3. Delete cookie s: just set the specified date to the past time

var d = new Date();
		d.setDate(d.getDate() - 1);//Get the specified date (cannot get previous date)
		document.cookie = "zxc=567;path=/;expires=" + d;

Be careful:
Only one cookie with a duplicate name can exist in the same path, and a duplicate setting is equivalent to a modification

document.cookie = "a=10";
document.cookie = "a=20";

Different paths can have cookie s with duplicate names

document.cookie = "a=20";
document.cookie = "a=30";
document.cookie = "a=20;path=/";

Encapsulation of cookie s:
1. Settings encapsulation of cookie s:

function setCookie(key,val,ops){
			ops = ops || {};
			let e = "";
				var d = new Date();
				d.setDate(d.getDate() + ops.expires);
				e = ";expires="+d;
			let p = ops.path ? ";path="+ops.path : "";
			document.cookie = `${key}=${val}${p}${e}`;

One of the ways to use it:
Set a cookie with validity and path


2. Capturing and encapsulating cookie s:

		function getCookie(key){
			// 1. Get all cookie s
			let strC = document.cookie;
			// 2. Use';'to separate all cookie s and get each one separately
			let arrC = strC.split("; ");
			// 3. Traverse through each cookie
			for(var i = 0;i<arrC.length;i++){
				// 4. Use'='here to separate names and values into separate states
				// 5. Determine if the first name of the array matches the name of the incoming cookie
				if(arrC[i].split("=")[0] === key){
					// 6. If consistent, return the second bit of the array, which is the corresponding value
					return arrC[i].split("=")[1];
			// 7. After the loop ends, if the program is still executing and no consistent value is found, an empty character is returned.
			return "";

Example usage:


3. Encapsulation for cookie deletion:

function removeCookie(key,ops){
			// 1. Make sure ops are an object
			ops = ops || {};
			// 2.ops is an object, there must be an expires attribute of -1 anyway
			ops.expires = -1;
			// 3. Give setCookie the processed ops
			// The second value here cannot be omitted

Example usage:

Published 9 original articles, won 13 reviews, and visited 232
Private letter follow

Tags: Session Attribute

Posted on Tue, 10 Mar 2020 21:20:01 -0400 by mabus