/**
* 创建cookie
*/
function _setCookie (cname, cvalue, exdays) {
var d = new Date()
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000))
var expires = 'expires' + d.toGMTString()
document.cookie = cname + '=' + cvalue + '; ' + expires
}
/**
* 获取cookie
*/
function _getCookie (cname) {
var name = cname + '='
var ca = document.cookie.split('; ')
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim()
if (c.indexOf(name) == 0) return c.substring(name.length, c.length)
}
return ''
}
/**
* 删除cookie
*/
function _delCookie (name) {
var exp = new Date()
exp.setTime(exp.getTime() - 1)
var cval = getCookie(name)
if (cval !== null) {
var expires = 'expires' + d.toGMTString()
document.cookie = name + '=' + cval + '; ' + expires
}
}
/**
* 清空所有cookie
*/
function _delAllCookie () {
var arrCookie = document.cookie.split('; ')//将多个cookie分割成多个键值对
for (var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split('=')
if (arr.length > 0) {
_delCookie(arr[0])
}
}
}
https://www.cnblogs.com/gg1234/p/5611086.html