首页 > 代码库 > cookie 设置有效期 检测cookie

cookie 设置有效期 检测cookie

设置cookie 函数直接上代码:

function setCookie(name, value, days) { //设置cookie
                    var d = new Date();
                    d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
                    var expires = "expires=" + d.toUTCString();
                    document.cookie = name + "=" + value + "; " + expires;
                }

设置cookie:

setCookie("key", ‘value‘, 1);

 

cookie检测:

document.cookie.indexOf("key")

返回值 -1表示不存在此cookie

cookie 设置有效期 检测cookie