Using decodeURI with javascript cookies

My problem is that I need a variable that will track all my cookies so that I can split the string in this variable into an array and then parse the string. I am wondering why the simple following code does not do this for me?

var count = 0;          //keeps track of how many times this page has been visited
var lastVisit = new Date();     //records the last visit date in UTC format (or extra-challenge: in a user-friendly format like "Tuesday 10/12/2013 at 9:34:50")
var exDate  = new Date(lastVisit.getTime() + 30000);
var savedData = decodeURI(document.cookie);  //contains cookie contents

document.cookie = encodeURI("count=" + count.toString() + "; expires=" + exDate.toUTCString());

I need that every time I set a cookie to add it to the savedData variable, I cannot understand why this is not happening. thank you

+4
source share
2 answers

'expires ='. "expires% 3D", . , "encodeURI", ";" ',' .

encodeURIComponent cookie, escape() cookie.

...

document.cookie = "count=" + encodeURIComponent(count.toString()) + "; expires=" + exDate.toUTCString();

... , .
; , .

( )


cookie

, RFC, , , , , .

cookie , , :
0x21-0x27, 0x2A-0x2B, 0x2D-0x2E, 0x30-0x39, 0x41-0x5A, 0x5E-0x7A 0x7E.
: :
0x00-0x20, '(', ')', ',', '/', ':', ';', '<', '=' ' > ', '?', '@', '\', '[', ']', '{', '}' 0x7F-0xFF.

cookie cookie-, , :
0x21, 0x23-0x2B, 0x2D-0x3A, 0x3C-0x5B, 0x5D-0x7E.
: :
0x00-0x20, 0x22, ',', ';', '\' strong > 0x7F-0xFF.

toUTCString(), .
: Wed, 09 Jun 2021 10:18:14 GMT - . ! , cookie-name cookie-.

: W3Schools , escape() JavaScript 1.5, encodeURI() encodeURIComponent() cookie. escape() cookie.

RFC 6265 5.4 :

. , cookie , . cookie
( ) (, ), , , UTF-8 [RFC3629] .
, UTF-8.

decodeURIComponent() 0x00 0xFF, . , unescape() , 8- , aka. , .

cookie Unicode, encodeURIComponent()/decodeURIComponent(), , , .

+7

btoa Base64 atob Base64. Base64 cookie ( ASCII UTF-8). , ( ) Base64 cookie, ( ), ( ) .

0

All Articles