0

I want to create a cookie with:

  • Name: drm
  • Value: drmStatus=Expected

I'm using the code from the answer to this question - Jquery Cookie plugin - multiple values? to create the cookie

 var obj = { drmStatus: 'Expected' }; 
 $.cookie('drm', $.param(obj), { path: '/', raw: true })

However this generates a cookie with

  • Name: drm
  • Value: drmStatus%3DExpected

The reason for this is this snippet from https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js

config.raw ? value : encodeURIComponent(value)

This is using the value of "raw" from the config object to decide whether to encode the value. It appears to be ignoring the value of "raw" value passed in the options object.

So my question is, can I set the option raw: true when using jquery.cookie?

Community
  • 1
  • 1
user1069816
  • 2,763
  • 2
  • 26
  • 43

1 Answers1

2

I think your problem is not in the jquery.cookie but in the $.param part of your script. See this JSFiddle. The raw option is set correctly and the cookie has your desired value.

This should work:

var obj = { drmStatus: 'Expected' }; 
$.cookie('drm', decodeURIComponent($.param(obj)), { path: '/', raw: true })

EDIT: I updated the Fiddle to work with your new version. The solution is:

var obj = { drmStatus: 'Expected' }; 
$.cookie.raw = true;
$.cookie('drm', $.param(obj), { path: '/' });
crackmigg
  • 5,571
  • 2
  • 30
  • 40
  • Thank you for your answer. The JSFiddle you supplied works perfectly, however the version of jquery.cookie.js is different. When I tried your example with the version from https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js it didn't work. Where did you get that version of the library from? I may change to that version if it solves my problem. – user1069816 Oct 24 '12 at 10:15
  • Yes, I did not realize the different version. My version is from here: (https://code.google.com/p/litepublisher/source/browse/trunk/js/plugins/jquery.cookie.js?spec=svn2794&r=2794). It seems to be an older version than yours. But it does not have the error of mixing up `config` and `options` variable. – crackmigg Oct 24 '12 at 16:28
  • I'm going to change to the version of jquery.cookie.js from [Litepublisher](https://code.google.com/p/litepublisher/source/browse/trunk/js/plugins/jquery.cookie.js) (which was in migg's original JSFiddle) as this seems more intuitive than having to set the config.raw value. – user1069816 Oct 25 '12 at 08:04