初识js cookie
cookie可以让你存储用户的信息在网页上
- 创建一个cookie
document.cookie="username=abc";
注意里面的name和value是不需要加引号的
- 可以设置一个过期时间(采用utc时间),默认的情况下,浏览器关闭之后就会删除cookie,因为他们的状态是session
document.cookie = "username=abc;expires= Ths,18 Dec 2013 12:00:00 UTC";
- 路径参数 ,你可以告诉浏览器cookie是属于哪个页面的,默认是当前页面
document.cookie = ‘username=chen;expires=Thu,18 Dec 2013 12:00:00 UTC;path=/‘;
- 读取cookie
var x = document.cookie;
此时,document.cookie将返回所有的cookie为一个字符串;例如:cookie1=value1;cookie2=value2
- 改变cookie 和创建的方法一致
document.cookie=‘username=acb;expires=Thu,18 Dec 2013 12:00:00 UTC;path=/‘;
- 删除cookie(只需要设置过期时间为已经过去的时间即可)
document.cookie= ‘username=abc;expires=Thu,01 Jan 1970 00:00:00;path=/;‘;
function setCookie(myname,myvalue,exday){
var myDate = new Date();
myDate.setTime(myDate.getTime()+exday*24*60*60*1000);
var expires = "expires="+myDate.toUTCString();
document.cookie = myname + "=" + myvalue +";"+expires + ";path=/";
}
function getCookie(myname){
var name = myname +‘=‘;
var decodedCookie = decodeURIComponent(document.cookie);
var multArr = decodedCookie.split(";");
for(var i=0;i<multArr.length;i++){
var arrItem = multArr[i];
while(arrItem.charAt(0) == ‘ ‘){
arrItem =arrItem.substring(1);
}
if(arrItem.indexOf(name) == 0){
return arrItem.substring(name.length,arrItem.length);
}
}
return ‘‘;
}
function checkCookie(){
if(getCookie(‘myset‘) !=‘‘){
console.log(‘yes‘);
}else{
setCookie(‘myset‘,‘myCustomer‘,30)
console.log(‘abc‘);
var popupWrapper = document.querySelector(".popup-wrapper");
var close1 = document.querySelector(‘.close-popup‘);
popupWrapper.style.display= "block";
close1.addEventListener(‘click‘,function(){
popupWrapper.style.display= "none";
})
}
}
window.onload=function(){
console.log(1);
checkCookie();
}