2

Hi I am new to Java Script and have a scenario I want to store some data on javascript cookies.

the probelm is javascript cookies can store only 4 KB of data. Looking for an alternate i found out jStorage and local-storage but these are not supported in all browser. Is there a work around for same.

if ($.cookie('test_data') == null ){
     test_data.push({
      qid: qid, 
      answer:  answer
      });
$.cookie("test_data",JSON.stringify(test_data) , {  expires: expires,   path: '/' });
  }
  else{
    test_data = JSON.parse($.cookie("test_data"));
    test_data = $.grep(test_data, function(e){ return e.qid != qid;   });
    test_data.push({
    qid: qid, 
    answer:  answer
    });
    $.cookie("test_data", JSON.stringify(test_data), {  expires: expires, path: '/' });
  }

This is the sample code I am using.

Here problem is if my answer is to big than cookie storage fails.I have to store group of answer for same.

Any help is appreciated.

Some ref Link I saw: Related Link

Community
  • 1
  • 1
Sonali Kakade
  • 21
  • 1
  • 2

2 Answers2

3

Cookies cannot be used for storing large amount of data as they get transmitted to the server with each request.

So is the question: do you need exactly cookies? Or just to store some data locally?

If later then check localStorage that is available in all modern browsers.

c-smile
  • 26,734
  • 7
  • 59
  • 86
1

I "think" you should create table in database to storage "data"

table "COOKIES_DATA" : [ data_id , data , data_create , date_expire ]

and cookies only store data_id in table.

When user visit, you could get data_id in cookies to get data in database