简体   繁体   English

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

[英]How to Store More then one data in cookie in Json using Javascript

what i need我需要的

  • i need to store the max of 3 events id in json string.我需要在 json 字符串中存储最多 3 个事件 ID。

     like: { 9,133,34}.
  • next then i want that when 4 th event id insert then it would override the 9 event id .接下来,我希望当第 4 个事件 id 插入时,它将覆盖第 9 个事件 id。 and so 5th will override the 133 and so on.所以 5th 将覆盖 133 等等。

  • scenario is like that when user land on page of its event id is stored in cookie.场景就像当用户登陆页面时,其事件 ID 存储在 cookie 中。 in json string, and so on user browser browser browse next event its id gets stored in json string and max limit of 3 events id storing.在 json 字符串中,等等用户浏览器浏览器浏览下一个事件,它的 id 存储在 json 字符串中,最大限制 3 个事件 id 存储。

  • i have reffer this note : Howe can I save more strings in cookie and place them in list?我有这个说明: 如何在 cookie 中保存更多字符串并将它们放在列表中?

i have tried like我试过

html 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 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;

i have tried我试过了

    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;

step 1 .第1步 。 store all browser ids into one array.将所有浏览器 ID 存储到一个数组中。

store 2. fetch all ids and store into cookie.存储 2. 获取所有 ID 并存储到 cookie 中。

code代码

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

 document.cookie = 'event_id=' + arr;

snapshot cookie browser快照 cookie 浏览器在此处输入图片说明

  • lastly after googling a lot tried but it also not working最后在谷歌搜索后尝试了很多但它也不起作用

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

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

Keyword: document.cookie expires: that is not must it is automatically reset cookie关键字:document.cookie expires:不一定是自动重置cookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM