在 Javascript 中设置多个 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16842226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting multiple cookies in Javascript
提问by Sinha
I'm trying to set multiple cookies in document.cookie
, but unfortunately only one is getting added.
我正在尝试在 中设置多个 cookie document.cookie
,但不幸的是只添加了一个。
I know there are multiple examples present on the 'Net for setting up these kind of cookies,and I followed one of them. But still I'm unable to set that out. I followed this linkto set my cookie.
我知道网上有多个用于设置此类 cookie 的示例,我遵循了其中一个示例。但我仍然无法确定。我按照此链接设置我的 cookie。
My Code:
我的代码:
function setCookie(start_time,end_session_time,total_time,flag,count){
var cookie_string = "start_time="+start_time;;
if(end_session_time) {
cookie_string +="; end_session_time="+end_session_time;
}
if(total_time){
cookie_string +="; total_time="+total_time;
}
if(flag){
cookie_string +="; flag="+flag;
}
if(count){
cookie_string +="; count="+count;
}
document.cookie =cookie_string ;
console.log(cookie_string);
console.log("document.cookie ="+ document.cookie);
}
The Output:
输出:
cookie_string :: start_time=1369926508266; flag=1; count=1
document.cookie =start_time=1369926508266;
回答by Maxim Krizhanovsky
Adding a cookie is performed via document.cookie = "name=value"
to add multiple keys, you should perform multiple assigments
添加 cookie 是通过document.cookie = "name=value"
添加多个键来执行的,您应该执行多个分配
function setCookie(start_time, end_session_time, total_time, flag, count) {
document.cookie = "start_time=" + start_time;
if (end_session_time) {
document.cookie = "end_session_time=" + end_session_time;
}
if (total_time) {
document.cookie = "total_time=" + total_time;
}
if (flag) {
document.cookie = "flag=" + flag;
}
if (count) {
document.cookie = "count=" + count;
}
console.log("document.cookie = " + document.cookie);
}
回答by crimson_penguin
Cookies are key value pairs (with some optional additional info added on, like the expiry date). To set more than one, you just set document.cookie
more than once. The ;
separator is used to specify the additional info, not to add more different cookies.
Cookie 是键值对(添加了一些可选的附加信息,例如到期日期)。要设置多个,您只需设置document.cookie
多个。该;
分离器用于指定的其他信息,而不是增加更多的不同的Cookie。
回答by Mateen
There you go a sample example to add, list and delete multiple cookies
有一个示例示例来添加、列出和删除多个 cookie
<!DOCTYPE html>
<html>
<head>
<script>
var n=1;
function addCookie(){
document.cookie=n+"="+n;n++;
}
function ListCookies(){
var result = document.cookie;
document.getElementById("p").innerHTML=result;
}
function removeCookies(){
//document.cookie="";
var result = document.cookie;
var cookieArray = result.split(";");
for(var i=0;i<cookieArray.length;i++){
var keyValArr = cookieArray[i].split("=");
document.cookie=keyValArr[0]+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
}
</script>
</head>
<body>
<button onclick='addCookie()'>ADD COOKIE</button><br>
<button onclick='ListCookies()'>LIST COOKIES</button>
<button onclick='removeCookies()'>REMOVE COOKIES</button>
<h1>RESULT:</h1>
<p id="p"></p>
</body>
</html>