【问题标题】:get and set cookie values in array position在数组位置获取和设置 cookie 值
【发布时间】:2012-03-24 05:19:01
【问题描述】:

好的,我有逻辑,但不确定如何写入 cookie 中用管道分隔的特定位置,|

例如 - 取一个带有 0|0|0|0 的 cookie

如果全局变量设置为 Y,如果 cookie 存在,则将值 c 和 d 写入第二和第三个位置(假设数组) - 如果 cookie 不存在,则使用 0|0|c 创建一个 cookie |d

如果全局变量为空,则将值 a 和 b 写入第 0 位和第 1 位(如果 cookie 存在) - 如果 cookie 不存在,则使用 a|b|0|0 创建一个 cookie

我了解获取 cookie,并拆分 cookie 以获取值,但不确定如何写入特定位置。我假设使用“加入”。

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

【问题讨论】:

    标签: javascript cookies join split setcookie


    【解决方案1】:

    假设你想要的 cookie 值被命名为 "pipes" 并且你的变量被命名为 globalVar,你可以这样做:

    var val = readCookie("pipes") || "";
    var valArray = val.split("|");
    if (valArray.length >= 4) {
        if (globalVar == "Y") {
            valArray[2] = 'c';
            valArray[3] = 'd';
        } else if (globalVar == null) {
            valArray[0] = 'a';
            valArray[1] = 'b';
        }
        val = valArray.join("|");
    } else {
        // fully formed cookie value didn't exist
        if (globalVar == "Y") {
            val = "0|0|c|d";
        } else {
            val = "a|b|0|0";
        }
    }
    createCookie("pipes", val, 365);
    

    我必须说,将这个管道值存储在 cookie 中非常不方便,并且需要的代码比仅存储和检索 cookie 中的多个值可能需要的代码更多。

    从您的 cmets 中,听起来数据是 storeID|storeLocation 并且您有其中两个。我建议你这样做:

    var store1 = readCookie("store1");    // "1640|Jacob's Point"
    var store2 = readCookie("store2");    // "2001|Fred's Point"
    

    或者,如果你想做一个函数,你可以这样做:

    function readStoreInfo(item) {
        var info = readCookie(item);
        if (info) {
            var data = info.split("|");
            return({id: data[0], name: data[1]});
        }
        return(null);
    }
    
    function writeStoreInfo(item, obj) {
        var val = obj.id + "|" + obj.name;
        createCookie(item, val, 365);
    }
    

    因此,要读取 store1 的数据,您只需调用:

    var store1Info = readStoreInfo("store1");
    store1Info.id = 123;
    writeStoreInfo("store1", store1Info);
    

    然后您就可以独立读写 store1 和 store2 的数据了。

    【讨论】:

    • 您对存储值有何建议 - 我的值是这种格式的字符串,例如:1640|Jacob's Point|2001|Fred's Point
    • 为了使用该数据,您必须将其拆分为四个部分。那么,为什么不将每个组件存储在它自己命名的 cookie 值中,然后您可以将每个组件检索到它自己的变量中。然后您不必为了存储和使用它们而将它们组合和分离。四个变量,四个 cookie 值。
    • 不幸的是,我必须将它们全部保存在一个 cookie 中 - 我想将数据分成两个 cookie,但是我们已经有太多 cookie,现在正在限制我们使用多少 cookie,等等。
    • @Jason - 从技术上讲,这只是一个巨大的 cookie 字符串。您从 cookie 字符串中读取的每个名称/值对只是较大 cookie 字符串的一部分。只要看看 readCookie 是如何工作的,就可以看到它的实际效果。但是,如果您不希望商店有两个名称/值对,那么您可以坚持第一种方式或做事以及更新一个商店的价值的额外复杂性。
    • 是的,但是由于 IE 和 20 个 cookie 的限制(我们的 cookie 疯狂地激增),我们有点靠墙,直到我们清理它们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 2017-04-15
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    相关资源
    最近更新 更多