js cookie常用操作

/**
 * [setCookie 设置cookie]
 * @param {[type]} name  cookie名称
 * @param {[type]} value cookie值
 * @param {[type]} time  时间(例如20s或1h)
 */
function setCookie(name, value, time) {
var strsec = getsec(time); var exp = new Date(); exp.setTime(exp.getTime() + strsec * 1); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); } /** * [getsec 获取毫秒数] * @param {[type]} str [description] * @return {[type]} [description] */ function getsec(str) { var str1 = str.substring(1, str.length) * 1; var str2 = str.substring(0, 1); if (str2 == "s") { return str1 * 1000; } else if (str2 == "h") { return str1 * 60 * 60 * 1000; } else if (str2 == "d") { return str1 * 24 * 60 * 60 * 1000; } } /** * [getCookie 读取cookies ] * @param {[type]} name [description] * @return {[type]} [description] */ function getCookie(name) { var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = document.cookie.match(reg)) return unescape(arr[2]); else return null; } /** * [delCookie 删除cookies ] * @param {[type]} name [description] * @return {[type]} [description] */ function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 60 * 60 * 1000); var cval = getCookie(name); if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/"; }

 

posted @ 2018-12-18 16:22  Mankii  阅读(134)  评论(0编辑  收藏  举报
返回顶部