N
N
noname22017-01-02 14:52:19
safari
noname2, 2017-01-02 14:52:19

Why are Russian words not added to cookies in safari, but are displayed in opera?

Adding cookies via javascript The data comes via json, so this is the output.

var req = jQuery.parseJSON(json);
var date = new Date(new Date().getTime() + 60*60*24*365*1000*10);
document.cookie = "city="+req.city+"; path=/; expires=" + date.toGMTString();
document.cookie = "id="+req.id+"; path=/; expires=" + date.toGMTString();
document.cookie = "hash="+req.hash+"; path=/; expires=" + date.toGMTString();
document.cookie = "text=казань; path=/; expires=" + date.toGMTString();

The difficulty is that after the event, Cyrillic data is not written to the cookie and is not displayed in safari. Moreover, safari sets the time of this data to the current session and deletes it after the browser is closed. Opera preserves them perfectly. What can be wrong?
Safari (city):
7152981ef35a4aceb4efcaa1bdf4b6af.png
Opera (city)
4f6a24335a284c8cb7bec24b53b84093.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Ineshin, 2017-01-02
@noname2

Before storing in a cookie, encode your string with encodeURIComponent
On reading, decode back with decodeURIComponent
Although modern cookies support Unicode, it's not always possible to write it there.
Check out this article: stackoverflow.com/questions/1969232/allowed-charac...
It all depends on the browser, Safari doesn't save UTF for example. So encode your strings.
To work with Cookies, it is best to use a special library that can do all this out of the box and has a convenient API: https://github.com/js-cookie/js-cookie

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question