Jojo01
Jojo01

Reputation: 1289

Javascript + characters get removed when alerting encrypted string from cookie

I have an encrypted string in a cookie, the string has been encrypted using CryptoJS. When i look at the cookie with a tool called CookiesManager, the cookie is normal. But when i alert it from the cookie all the + characters have been removed. I am using a jQuery plugin called jQuery Cookie. I have tried to set encoding to UTF-8, but the + characters are still removed. Code(Js):

<script type="text/javascript" charset="UTF-8">
var host = $.cookie("encrHost");
alert("Host: " + host);
</script>

What gets alerted:

Host: U2FsdGVkX18vi2P/aWBEA4AzwE4 oMDFP2 tucKLyKk=

Cookies Manager Screen Shot: Cookies Manager

Upvotes: 2

Views: 95

Answers (1)

michelem
michelem

Reputation: 14590

This is because by default the cookie is url encoded as stated in the plugin homepage:

By default the cookie value is encoded/decoded when writing/reading, using encodeURIComponent/decodeURIComponent. Bypass this by setting raw to true:

$.cookie.raw = true;

So just add $.cookie.raw = true before you call it and you should do the trick.

Upvotes: 2

Related Questions