博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS Cookie
阅读量:5882 次
发布时间:2019-06-19

本文共 3415 字,大约阅读时间需要 11 分钟。

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);}

转载于:https://www.cnblogs.com/allvie/p/3862525.html

你可能感兴趣的文章
劣质代码评析——猜数字问题(上)
查看>>
纸上谈兵: 栈 (stack)
查看>>
Windows phone8 基础篇(三) 常用控件开发
查看>>
Oracle学习笔记之五,Oracle 11g的PL/SQL入门
查看>>
大叔手记(3):Windows Silverlight/Phone7/Mango开发学习系列教程
查看>>
考拉消息中心消息盒子处理重构(策略模式)
查看>>
so easy 前端实现多语言
查看>>
【追光者系列】HikariCP源码分析之ConcurrentBag&J.U.C SynchronousQueue、CopyOnWriteArrayList...
查看>>
canvas系列教程05-柱状图项目3
查看>>
css绘制几何图形
查看>>
HTML标签
查看>>
理解JS中的Event Loop机制
查看>>
转载:字符编码笔记:ASCII,Unicode和UTF 8
查看>>
修复看不懂的 Console Log
查看>>
Android跨进程通信 AIDL使用
查看>>
ajax常见面试题
查看>>
结合kmp算法的匹配动画浅析其基本思想
查看>>
vue进行wepack打包执行npm run build出现错误
查看>>
【d3.js v4基础】过渡transition
查看>>
VUEJS开发规范
查看>>