关于cookie 保存操作动作的时间

//设置Cookies
var Cookies = {};

存入Cookies
Cookies.set = function (name, value) {
    var argv = arguments;
    var argc = arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
      ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
      ((path == null) ? "" : ("; path=" + path)) +
      ((domain == null) ? "" : ("; domain=" + domain)) +
      ((secure == true) ? "; secure" : "");
};

取出Cookies
Cookies.get = function (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var j = 0;
    while (i < clen) {
        j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return Cookies.getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
}

前台页面调用

var exdate=new Date();
        exdate.setDate(exdate.getDate()+365);  //设置保存时间
        Cookies.set("receiptform", $("#OkIsGenType input[name=OkIsGen]:checked").val(), exdate);

//页面加载取出cookies 
设置值

if (Cookies.get("receiptform") != undefined) {
            $("#OkIsGenType input[name=OkIsGen][value=" + Cookies.get("receiptform") + "]").prop("checked", true);
        }

原文地址:https://www.cnblogs.com/Davi123/p/7324992.html