存cookie封装
/**存cookie */ function setCookie(objName, objValue, objHours) { var str = objName + "=" + escape(objValue); //编码 if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失 var date = new Date(); var ms = objHours * 3600 * 1000; date.setTime(date.getTime() + ms); str += "; expires=" + date.toGMTString(); } document.cookie = str; }
取cookie封装
/**取cookie */ function getCookie(objName) {//获取指定名称的cookie的值 var arrStr = document.cookie.split("; "); for (var i = 0; i < arrStr.length; i++) { var temp = arrStr[i].split("="); if (temp[0] == objName) return unescape(temp[1]); //解码 } return ""; }
js存cookie原理
/**js存cookie */ document.cookie = "key=value";