Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js保存Cookies #81

Open
gaowei1012 opened this issue Dec 14, 2021 · 0 comments
Open

js保存Cookies #81

gaowei1012 opened this issue Dec 14, 2021 · 0 comments

Comments

@gaowei1012
Copy link
Owner

保存Cookies###

function setCookies(c_name, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate + expiredays); document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()); }

获取Cookies

function getCookie(c_name) { if (document.cookie.length>0)//首先查询cookie是否是空的 { c_start=document.cookie.indexOf(c_name + "=")//检测这个cookie是否存在 if (c_start!=-1)//如果cookie存在 { c_start=c_start + c_name.length+1 //获取到cookie的值的开始位置 c_end=document.cookie.indexOf(";",c_start)//从c_start开始查找";"的存在 if (c_end==-1) c_end=document.cookie.length//如果没找到,说明是最后一项 return unescape(document.cookie.substring(c_start,c_end))//把cookie的值拆分出来并且对这个值进行解码,unescape()与escape()相对,对被escape()编码的字符串进行解码 } } return ""//不存在就返回空 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant