点击县旗时设置cookie

时间:2014-01-12 10:49:36

标签: javascript cookies

我正在尝试在语言发生变化时为我的网站设置一些cookie,但它不起作用。

以下是网站:http://www.goodyearwines.com/

这是我使用的代码:

function setCookies(name, value, expires, path, domain, secure){
    document.cookie = name + "=" + escape(value) + "; ";

    if(expires){
        expires = setExpiration(expires);
        document.cookie += "expires=" + expires + "; ";
    }
    if(path){
        document.cookie += "path=" + path + "; ";
    }
    if(domain){
        document.cookie += "domain=" + domain + "; ";
    }
    if(secure){
        document.cookie += "secure; ";
    }
}


$( "a.qtrans_flag_en" ).click(function() {
    setCookies('thelanguage', 'eng', 3, '/', 'goodyearwines.com');
});

$( "a.qtrans_flag_zh" ).click(function() {
    setCookies('thelanguage', 'chi', 3, '/', 'goodyearwines.com');
});

$( "a.qtrans_flag_fr" ).click(function() {
    setCookies('thelanguage', 'fra', 3, '/', 'goodyearwines.com');
});

欢迎任何帮助。感谢

1 个答案:

答案 0 :(得分:2)

document.cookie不是一个简单的变量,追加它可能不起作用。而是尝试使用它:

function setCookies(name, value, expires, path, domain, secure){

    var cookie = name + "=" + escape(value) + "; ";

    if(expires){
        expires = setExpiration(expires);
        cookie += "expires=" + expires + "; ";
    }
    if(path){
        cookie += "path=" + path + "; ";
    }
    if(domain){
        cookie += "domain=" + domain + "; ";
    }
    if(secure){
        cookie += "secure; ";
    }

    document.cookie = cookie;
}

并定义函数setExpiration()