简体   繁体   中英

How to use multiple document.cookie in JavaScript?

Multiple document.cookie is not working for me.

My code:

var objFecha = new Date();
objFecha.setTime(objFecha.getTime() + (86400 * 1000));
var strExpiracion = objFecha.toGMTString();
document.cookie = 'MyCookie;expires=' + strExpiracion;

This code works me, but the other does not:

var objFecha = new Date();
objFecha.setTime(objFecha.getTime() + (86400 * 1000));
var strExpiracion = objFecha.toGMTString();
document.cookie = 'MyCookie;expires=' + strExpiracion;

var objFecha2 = new Date();
objFecha2.setTime(objFecha2.getTime() + (30 * 1000));
var strExpiracion2 = objFecha2.toGMTString();
document.cookie = 'MyCookie2;expires=' + strExpiracion2;

The problem is that you actually create 2 cookies but with the same name (empty name), so this cookie always has "MyCookie2" as the value.

You need to add a = operator when setting it (even for empty values), like this:

document.cookie = 'MyCookie=;expires=' + str;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM