Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want unescaped characters in my cookie (jquery)

I want to store the href value in a cookie, trouble is when i do the cookie is escaping the forward slashes, so for example

code is

$.cookie ("mycookie", $link.attr("href"), { path: '/', expires: 7 });

html is

<li><a id="czechrepublic" href="/cz/cz.html">Česká republika</a></li>

When i store the href it is being stored as

%2Fcz%2Fcz.html

But i need it to be stored as /cz/cz.html is there a way of UNescaping characters in Jquery, i have seen this in standard javascript cookie tutorials but i am not sure how to do it with the Jquery cookie plugin

Thanks

Joe

like image 522
Joe Morris Avatar asked Oct 27 '09 15:10

Joe Morris


3 Answers

$.cookie.raw = true
$.cookie('mycookie','/cz/cz.html')

From here

like image 166
sites Avatar answered Oct 18 '22 18:10

sites


Use decodeURIComponent(vartobedecoded.replace(/\+/g, " "));

like image 42
Cesar Avatar answered Oct 18 '22 19:10

Cesar


When you retrieve the cookie through the jQuery cookie plugin via $.cookie('mycookie'), it will automatically be unescaped using the correct function which is decodeURIComponent.

If you need to decode the string at the server end, you'll have to use the URL-decoding function in whichever language you're using.

If you need to have an unencoded cookie because the server end can't be changed to use encoded cookies, you should forget the jQuery plugin andjust set the cookie yourself:

document.cookie= 'mycookie='+$link.attr("href");

For the value /cz/cz.html this will be OK, but there are lots of other characters you can't store in a cookie, which is why jQuery escapes them.

like image 35
bobince Avatar answered Oct 18 '22 20:10

bobince