R
R
Roman2016-06-25 09:32:10
JavaScript
Roman, 2016-06-25 09:32:10

How to get all COOKIES with NAME starting with WORD?

When users click "add to favorites" a cookie is created like:
name: like_id - where id is either from 1 to 6 digits, or 'undefined'
value: 1 I
form a link like:
site.com?ids=id1+id2+id3

function favorite() {
  var arrCook = document.cookie.split(';');
  var likeCook = arrCook.filter(function(c) {
    return c.trim().indexOf('like_') === 0;
  });
  var mapArr = likeCook.map(function(c) {
    var subresult = c.match( /like_(.*)=1/ );
    return subresult[1];
  });
  var args = Array.prototype.slice.call(mapArr);
  var result = args.join('+');
  
  if (result !== '') {
    $("#menu").prepend('<li id="menu_favorite"><a href="http://site.com?ids=' + result + '&favorite">Избранное</a></li>');
  }
}
favorite();

How to improve the code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2016-06-25
@ACCNCC

var newarr=[];
for and condition:
if(typeof COOKIES_list[i].id !== "undefined"){
// COOKIES_list[i].id is not undefined
// push to a new array where cookies without id are undefined
newarr.push(COOKIE[i] );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question