把一个哈希放在javascript cookie中

时间:2011-03-11 06:58:12

标签: javascript cookies hash

我想在javascript cookie中包含一个哈希值,因为我有一个要记住的事项列表......任何人都可以设置如何设置cookie以及检索它的方法吗?

1 个答案:

答案 0 :(得分:4)

您可以在JavaScript中设置Cookie,如下所示:

document.cookie = 'somekey=somevalue; expires=Thu, 2 Aug 2011 21:17:11 UTC; path=/'

创建,阅读和创建的代码用JavaScript擦除cookie(Quirksmode):

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