简体   繁体   English

设置“document.cookie”

[英]Setting “document.cookie”

Trying to set cookie below ways and facing issues:尝试以下列方式设置 cookie 并面临问题:

Option1:选项1:

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

This sets value only till name=value when i recall document.cookie.当我想起 document.cookie 时,这只会设置值直到 name=value。

Option 2:选项 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 ";"这工作正常,我能够读取基于“;”的 cookie 中的所有值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.在 sonarqube 报告中,它说像选项 2 中的那样分配 document.cookie 是错误的,并且是一个错误。

Only the key/value pairs are exposed from document.cookie只有键/值对从document.cookie中公开

This is done using JavaScript Object Accessors这是使用 JavaScript Object 访问器完成的

Option 1 is working, check your developer tools选项 1 有效,请检查您的开发人员工具

Option 2 is actually not what you want.选项 2 实际上不是您想要的。 Each assignment creates a new cookie.每个分配都会创建一个新的 cookie。 Three of them are created with respectively name , expires and path as cookie names.其中三个分别使用name 、 expires 和 path 作为 cookie 名称创建。 What you see from document.cookie is misleading您从document.cookie中看到的内容具有误导性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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