I am creating an array to store in cookie using jquery.cookies.js
Below is the way I am storing and accessing values from an array
var th = $(this);
var TempRecord = []; // Defining an Array to store all attributes
var Q = th.find('Name').first().text();
var NR = th.attr('NotReq');
var FieldWidth = th.attr('FWidth');
And Pushing elements into an array like this
if(th.attr('FieldWidth'))
{
TempRecord.push("Field Width");
TempRecord.push(FieldWidth);
}
Then joining the array as below to store in an array as a string
var temp = TempRecord.join('splitter'); Then storing into cookie...
// Like the above I am storing all the other attribute values
And accessing them like below
var rec = temp.split('splitter');
for(var i=0;i<rec-1;i=i+2)
{
var x = rec[i];
var y = rec[i+1];
}
Please suggest me a way improve the above code. And also please remember that I am gonna store this in cookie.
You could use JSON for serialising your data, something like:
And then to get it out again:
In order to ensure cross-browser compatibility for
JSON.parse()andJSON.stringify()you would need to include json2.js.