HTTP协议
HTTP:超文本传输协议,用于web服务器传输到本地浏览器的传输协议,它是一个无状态协议
cookie
是指缓存在本地客户端的数据
//查询cookie
console.log(document.cookie)
//设置cookie
document.cookie = "username = honny"
var date = new Date();
date.setDate(date.getDate()+3)
document.cookie = "username=honny;expires="+date;//设置到期时间为三天后
//修改cookie
document.cookie = "username = honny"
document.cookie = "username = honny2"
//删除cookie
var date = New Date()
date.setDate(date.getDate()-2)
document.cookie = "username = honny;expries="+date;//设置一个过去时间即为删除
cookie操作的封装
setCookie()
var setCookie = function(name,value,day){
var date = new Date()
date.setDate(date.getDate()+day)
document.cookie = name +'='+ value +';expries='+ day
}
setCookie(name,honey,3);
console.log(document.cookie)
getCookie()
var getCookie = function(name){
var str = document.cookie;
var arr = str.split(';');
for(var i = 0; i < arr.length; i++){
var arr1 = arr.split('=')
if(arr1[0] == name){
return arr1
}
}
}
consloe.log(getCookie('name2'));
removeCookie
var removeCookie = function(name) {
setCookie(name,value,-1)
}