如何使用Javascript在Json中的cookie中存储多个数据

时间:2015-05-15 06:14:02

标签: javascript jquery json cookies

我需要什么

  • 我需要在json字符串中存储最多3个事件id。

        like: { 9,133,34}.
    
  • 接下来我希望当第4个事件id插入时它会覆盖9事件id。因此,第五将覆盖133,依此类推。

  • 场景就像当用户登陆其事件ID的页面存储在cookie中时。在json字符串中,等用户浏览器浏览器浏览下一个事件,其id存储在json字符串中,并存储3个事件id的最大限制。

  • 我已经提到了这个说明:Howe can I save more strings in cookie and place them in list?

我试过像

HTML

                <input type=hidden id="eventID" value="{{ data.detailes.data.event.event_id }}">

            <input type=hidden id="industry_id" value="{{data.detailes.data.industry_id.id}}">

js code

    var cookie_event_id=document.getElementById("eventID").value;
   alert(cookie_event_id);
 document.cookie = 'event_id=' + cookie_event_id;

  var cookie_industry_id=document.getElementById("industry_id").value;
   alert(cookie_event_id);
  document.cookie = 'industry_id=' + cookie_industry_id;

我试过了

    var cookie_event_id=document.getElementById("eventID").value;
 var cookieevent = [event_id, '=', JSON.stringify(cookie_event_id), ';
         domain=.', window.location.host.toString(), '; path=/;'].join('');

        document.cookie = 'event_id=' + cookieevent;

第1步。将所有浏览器ID存储到一个数组中。

存储2.获取所有ID并存储到cookie中。

代码

 var arr = [];
 // append new value to the array
  arr.push(cookie_event_id);
 console.log(ids.toString());

 document.cookie = 'event_id=' + arr;

快照cookie浏览器 enter image description here

  • 最后谷歌搜索了很多尝试,但它也没有工作

      var arr[];
     document.cookie = 'event_id=' + serializeArray().map(function(x){arr[x.eventid] = x.value;
    

1 个答案:

答案 0 :(得分:0)

的document.cookie =&#34;名称=样品; expires =星期四,2015年12月18日12:00:00 UTC;路径= /&#34 ;;

关键字:document.cookie 到期:这不是必须自动重置cookie