cookie usage and packaging

The concept cookie:
refer to certain websites in order to identify the user's identity, a session (sessions) and track data (usually encrypted) on the user's local terminal storage. Exist in text form.
Technical session tracking cookie:
tracking every connection, connection record information generated, the next connection, along with the past sent
Features :
1. only save characters, text
2. does not allow cross-domain (who exist, and who use)
3 time critical, the default session level, close the browser, can be deleted (valid for a limited time, i.e. aging property)
4. number (50) and size (4K) limits
the use of the cookie:
1, set default cookie:

document.cookie = "user=admin";
		document.cookie = "pass=123";
		console.log(document.cookie);

When acquiring cookie, document.cookie will get a one-time all cookie, the results we return to the browser is based on the following format presents
user = admin; pass = 123
but does not affect the result set format, set the time settings or how how to set
does not allow more than a one-time deposit

document.cookie = "a=10;b=20;c=30"; //只会生效第一个
		console.log(document.cookie);

2, is provided with a validity period of the cookie

var d = new Date();
		d.setDate(d.getDate() + 3);//获取指定日期
		document.cookie = "abc=123;expires=" + d;
		document.cookie = "qwe=345;path=/";
		console.log(document.cookie);

Note:
when query cookie, the cookie can not get the current path is valid and
detailed information a cookie can only be viewed by a tool (browser-based console)
3, delete the cookie: as long as the specified date in the past time to

var d = new Date();
		d.setDate(d.getDate() - 1);//获取指定日期(无法获取以前的日期)
		document.cookie = "zxc=567;path=/;expires=" + d;
		console.log(document.cookie)

: Note that
there is a cookie only under the same name of the same path, duplication equivalent modifications

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

There may be different paths cookie of the same name

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

cookie package:
. 1, cookie package provided:

function setCookie(key,val,ops){
			ops = ops || {};
			let e = "";
			if(ops.expires){
				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 method to use:
set a cookie both have valid path

setCookie("hahhah","677",{
			expires:3,
			path:"/"
		});

2, cookie package acquisition:

		function getCookie(key){
			// 1.获取所有cookie
			let strC = document.cookie;
			// 2.使用"; "分隔所有的cookie,单独拿到每一条
			let arrC = strC.split("; ");
			// 3.遍历每一条cookie
			for(var i = 0;i<arrC.length;i++){
				// 4.在此使用"="分隔,分隔成 名字和值的独立的状态
				// 5.判断数组的第一位的名字时否与传进来的获取的cookie的名字一致
				if(arrC[i].split("=")[0] === key){
					// 6.如果一致,返回数组的第二位,也就是对应的值
					return arrC[i].split("=")[1];
				}
			}
			// 7.循环结束后,如果程序还在执行,说明没有找到一致的值,那就返回空字符
			return "";
		}

Use example method:

console.log(getCookie("qwe"));
console.log(getCookie("abc"));
console.log(getCookie("user"));

3, cookie deletion package:

function removeCookie(key,ops){
			// 1.保证ops是个对象
			ops = ops || {};
			// 2.ops是对象了,无论如何得有个expires的属性为-1
			ops.expires = -1;
			// 3.将处理好的ops,给setCookie
			setCookie(key,"hsa",ops);
			// 这里的第二个值不能省略
		}

Use example method:

removeCookie("abc",{
			path:"/"
		});
Released nine original articles · won praise 13 · views 272

Guess you like

Origin blog.csdn.net/qq_44732010/article/details/104786444