简体   繁体   中英

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.

     like: { 9,133,34}.
  • next then i want that when 4 th event id insert then it would override the 9 event id . and so 5th will override the 133 and so on.

  • scenario is like that when user land on page of its event id is stored in 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.

  • i have reffer this note : Howe can I save more strings in cookie and place them in list?

i have tried like

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;

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 . store all browser ids into one array.

store 2. fetch all ids and store into 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在此处输入图片说明

  • 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=/";

Keyword: document.cookie expires: that is not must it is automatically reset cookie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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