Possible Duplicate:
Allowed characters in cookies
I’m using JSON.stringify to convert an object to save it in a cookie. But after saving Arabic Windows-1256 encoding in a cookie, I wasn’t able to restore it. Here’s what I did:
For example:
Convert and save in a cookie.
conv[user] = {"user":1,"u":1,"m":3,"c":255,"comment":'السلام عليكم ورحمه الله'};
addCookie('chat_conversations', JSON.stringify(conv) , 7);
Restore value from cookie:
var con = is_cookie('chat_conversations');
conv = jQuery.parseJSON(con);
Getting my JSON result:
alert(conv[1].comment);
Result
“‘D3D’E 9DJCE H1-EG ‘DDG H(1C’*G\n”
Here’s the result of my cookie
chat_conversations={“1”:{“user”:”1″,”u”:”1″,”m”:3,”c”:255,”comment”:”‘D3D’E 9DJCE H1-EG ‘DDG H(1C’*G\n”}}; expires=Sat, 08 Dec 2012 15:00:42 GMT; path=/; domain=127.0.0.1
How can I save an object containing Arabic in a cookie and restore it?
You should sanatise strings going into a cookie using
escapeorencodeURIComponent(minor differences between the two, see below) and then reverse this viaunescapeordecodeURICompontentwhen retrieving the data.escapeis fine because you just want to use this data in JavaScript. If you want to access the cookie server-side then I’d strongly recommend usingencodeURIComponent.If you are just using it for JavaScript, consider
window.localStoragebecause it will cause less internet usage.