Can you store commas with spaces inside cookies?

I recently played with cookies, and I noticed that when I create one with the value "Hello, World!" I get "Hello":

document.cookie = "testCookie=Hello, World!;";

Interestingly, if I do not add a space to "Hello, World!" , the cookie value will remain as I set it: ("Hello, World!")

document.cookie = "testCookie=Hello,World!;";

My only guess is why this happens because the comma and space β€œ,” end cookies, such as semicolons and spaces, β€œdo.” Is this true, or am I doing something wrong?

+4
source share
2 answers

You should use encodeURIComponentfor MDN status value :

cookie encodeURIComponent(), , , ( cookie).

+3

, cookie, de RFC 6265, .

cookie:
*
*

cookie < >
*
*


, cookie, , , , , undefined.


, js-cookie, jquery.cookie, , , , . .

0

Source: https://habr.com/ru/post/1612949/


All Articles