【问题标题】:How to Store More then one data in cookie in Json using Javascript如何使用 Javascript 在 Json 中的 cookie 中存储多个数据
【发布时间】:2015-05-15 17:45:44
【问题描述】:

我需要什么

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

        like: { 9,133,34}.
    
  • next 然后我希望在插入第 4 个事件 id 时它会覆盖第 9 个事件 id。所以 5th 将覆盖 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代码

    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 浏览器

  • 最后在谷歌上搜索了很多尝试但它也不起作用

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

【问题讨论】:

    标签: javascript jquery json cookies


    【解决方案1】:

    document.cookie="Name=sample; expires=Thu, 18 Dec 2015 12:00:00 UTC; path=/";

    关键字:document.cookie expires:不是必须自动重置cookie

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 2015-05-22
      相关资源
      最近更新 更多