优秀前端工程师必备: cookie的增删改查Demo!
1 cookie可以很好地解决微信浏览器登录状态的保存,具体教程看下面链接:
https://www.cnblogs.com/autoXingJY/p/10456767.html
2 参考了w3c等的一些内容,总结了下自己经常用js进行的cookie操作:"cookie的 添加 , 获取, 删除":
2-1 添加cookie
function setCookie(c_name, value, expiredays) {
var exdate = new Date()
exdate.setDate(exdate.getDate() + expiredays)
document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}
2-2 获取cookie
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=")
if (c_start != -1) {
c_start = c_start + c_name.length + 1
c_end = document.cookie.indexOf(";", c_start)
if (c_end == -1) c_end = document.cookie.length
return unescape(document.cookie.substring(c_start, c_end))
}
}
return ""
}
2-3 删除cookie
function delCookie(uname) {
setCookie(uname, "", -1);
}