首页 > 代码库 > JS Cookie
JS Cookie
function addCookie(objName, objValue, objHours) { var str = objName + "=" + EncodeURI(objValue); if (objHours > 0) { var date = new Date(); var ms = objHours * 3600 * 1000; date.setTime(date.getTime() + ms); str += "; expires=" + date.toGMTString(); } document.cookie = str;}function getCookie(name) { var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) return DecodeURI(arr[2]); return null;}function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval = getCookie(name); if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();}var EncodeURI = function (unzipStr, isCusEncode) { if (isCusEncode) { var zipArray = new Array(); var zipstr = ""; var lens = new Array(); for (var i = 0; i < unzipStr.length; i++) { var ac = unzipStr.charCodeAt(i); zipstr += ac; lens = lens.concat(ac.toString().length); } zipArray = zipArray.concat(zipstr); zipArray = zipArray.concat(lens.join("O")); return zipArray.join("N"); } else { //return encodeURI(unzipStr); var zipstr = ""; var strSpecial = "!\"#$%&‘()*+,/:;<=>?[]^`{|}~%"; var tt = ""; for (var i = 0; i < unzipStr.length; i++) { var chr = unzipStr.charAt(i); var c = StringToAscii(chr); tt += chr + ":" + c + "n"; if (parseInt("0x" + c) > 0x7f) { zipstr += encodeURI(unzipStr.substr(i, 1)); } else { if (chr == " ") zipstr += "+"; else if (strSpecial.indexOf(chr) != -1) zipstr += "%" + c.toString(16); else zipstr += chr; } } return zipstr; }}var DecodeURI = function (zipStr, isCusEncode) { if (isCusEncode) { var zipArray = zipStr.split("N"); var zipSrcStr = zipArray[0]; var zipLens; if (zipArray[1]) { zipLens = zipArray[1].split("O"); } else { zipLens.length = 0; } var uzipStr = ""; for (var j = 0; j < zipLens.length; j++) { var charLen = parseInt(zipLens[j]); uzipStr += String.fromCharCode(zipSrcStr.substr(0, charLen)); zipSrcStr = zipSrcStr.slice(charLen, zipSrcStr.length); } return uzipStr; } else { //return decodeURI(zipStr); var uzipStr = ""; for (var i = 0; i < zipStr.length; i++) { var chr = zipStr.charAt(i); if (chr == "+") { uzipStr += " "; } else if (chr == "%") { var asc = zipStr.substring(i + 1, i + 3); if (parseInt("0x" + asc) > 0x7f) { uzipStr += decodeURI("%" + asc.toString() + zipStr.substring(i + 3, i + 9).toString());; i += 8; } else { uzipStr += AsciiToString(parseInt("0x" + asc)); i += 2; } } else { uzipStr += chr; } } return uzipStr; }}var StringToAscii = function (str) { return str.charCodeAt(0).toString(16);}var AsciiToString = function (asccode) { return String.fromCharCode(asccode);}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。