Serialize json form and store in cookie

I want to save the form on clientide, in json in the cookie, after that deserialize it back to the form. What am I doing:

serialization in JSON:

function formToJSON(selector) { var form = {}; $(selector).find(':input[name]:enabled').each(function () { var self = $(this); var name = self.attr('name'); if (name.indexOf('TextBox', 0) == 0) { if (form[name]) { form[name] = form[name] + ',' + self.val(); } else { form[name] = self.val(); } } }); return form; } 

then when changing the form, I try to save the form in a cookie:

  $('#form1 :input').change(function () { var eba = formToJSON($('#form1')); $.cookie.set('fo', eba, {json:true}); var a = $.cookie.get('fo',true); alert(a); //$.cookie.set('form123', { "ksf": "saf", "tt": "" }, { json: true }); //var b = $.cookie.get('form123', true); //alert(JSON.stringify(b)); }); 

in the debugger - eba is a json object, but alert (a) indicates null. commented code works, this json series is serialized and I get it from cookies. but why does the code not work for the form ??? cookie plugin taken from jquery.com

0
json javascript jquery serialization cookies
Mar 28 '11 at 12:05
source share
2 answers

Use this library for JSON string / parsing http://json.org/js.html

remember that cookies are about 4 KB in size, http://support.microsoft.com/kb/306070

+2
Mar 28 '11 at 12:09
source share

AFAIK browser cookies cannot be read using javascript (with the exception of your own domain) to prevent the Reeseqest Subtitle for cross-referencing But you can still set them.

-2
Mar 28 '11 at 12:09
source share



All Articles