A
A
Alexey2017-08-23 23:04:39
JavaScript
Alexey, 2017-08-23 23:04:39

Why are cookies saved in this form?

I save cookies like this:

var l = e.currentTarget.dataset.lang,
                expires = "",
                date = new Date();

            date.setTime(date.getTime() + (360*24*60*60*1000));
            expires = "; expires=" + date.toUTCString();
            document.cookie = 'language' + "=" + l + expires + "; path=/";

The result itself should include the value and the key, namely:
language = ru

But what I see is a completely different result, as if I need to convert to a different encoding or deserialize:
language = d725e8fc6e6a3c9370602caf9b8157cfc1f69ba22fdef33654c57f769a7f9012a%3A2%3A%7Bi%3A0%3Bs%3A8%3A%22language%22%3Bi%3A1%3Bs%3A2%3A%22en%22%3B%7D

Questions: Why is this kind of value? How to decode it or what am I doing incorrectly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kovalsky, 2017-08-23
@azovl

If you manually set element.dataset.lang , then that is where the error lies. You probably initially write such a wild value in the data-lang of your element.
If we are talking about the language, then you can take it from html , that is, document.documentElement.lang , something like this:

var l = document.documentElement.lang,
    expires = "",
    date = new Date();

date.setTime(date.getTime() + (360*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
document.cookie = 'language' + "=" + l + expires + "; path=/";

// "language=ru; expires=Sat, 18 Aug 2018 20:30:14 GMT; path=/"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question