Reputation: 4014
So I'm using cookies for my website (big surprise ;-) ), but what might be surprising is that I for some reason (don't ask me why) store some css
style values in the cookie, which is all fine and dandy until you add a ;
in there which is not supposed to be there.
So the cookie I've stored looks like this:
token=9d133691-a955-4980-bf1d-9492d3c64026;
user=2601;
config=JSON"{
"socketAddress":"string",
...
bunch more values
...
"views":[{
"showInMenu":true,
"route":"Opgave Liste",
"routeParameters":{"afdeling":true},
"viewFunctions":{
"itemList":{
"css":{
"class":"bold",
"conditional":{
"condition":"[REG.TASK] === 'off'",
"false":{
"style":"background:red;"
}
}
}
}
}
}]
}";
and the way I get the cookie is like this:
<script>
function readCookie(){
var cookie = document.cookie;
console.info('base cookie : ', cookie);
}
readCookie();
</script>
the result i get is the right cookie, it however is cut right after background:red
. As I mentioned earlier I know why, my question is, is there a way to prevent this and still have the ;
in the string. is there some kind of escape character like you have in string: ie. \"
?
Upvotes: 0
Views: 49
Reputation: 19719
Try using encodeURIComponent(css)
before doing the JSON stringification, and then using decodeURIComponent
in order to get the real value.
For example:
{
"style": encodeURIComponent("background:red;")
}
(You can/should automate this process with utility functions).
Upvotes: 1