简体   繁体   中英

Setting “document.cookie”

Trying to set cookie below ways and facing issues:

Option1:

document.cookie = name + "=" + value + "; expires=" + date.toUTCString() + "; path=/";

This sets value only till name=value when i recall document.cookie.

Option 2:

document.cookie = "${name}=value";   
document.cookie = "expires=${date.toUTCString()}";
document.cookie = "path=/";

This works fine and i am able to read all the values from cookie based on ";"split.

Why this odd behaviour? And in sonarqube report it says assigning document.cookie like the one in option 2 is wrong and its a bug.

Only the key/value pairs are exposed from document.cookie

This is done using JavaScript Object Accessors

Option 1 is working, check your developer tools

Option 2 is actually not what you want. Each assignment creates a new cookie. Three of them are created with respectively name , expires and path as cookie names. What you see from document.cookie is misleading

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