2

I have cookies with values in it, and between values I put # so I can later extract data from cookies.

Now I need a way to search for cookies which begin with some word let say "word", and when I find that cookies I want to extract word from it and as I sad char # is indicator for that and char % is indicator for last char.

var name = "word" + "" + n1 + "" + n2 + "" + n3;
var value = v1 + "#" + v2 + "#" + v3 + "#" + v4 + "%";

name is cookie name, and value is cookie value.

I need function which will look for all cookies which begin with "word" and when find it, from cookie value extract v1 v2 v3 and v4 in 4 different variables.

I have to look all cookies names and if cookie name begin with "word" like

"word and here is the rest of the string".

That is first part.

Second part is when we find cookies with that name, now from cookie value which is string separated with # we need to separate 4 variables.

Is it now clear what I need?

tanasi
  • 1,804
  • 6
  • 37
  • 53

1 Answers1

3

Two helper functions.

One that decodes the document.cookie string into an object with keys/values:

function decodeCookie() {
  var cookieParts = document.cookie.split(";"), 
  cookies = {};

  for (var i = 0; i < cookieParts.length; i++) {
    var name_value = cookieParts[i],
        equals_pos = name_value.indexOf("="),
        name       = unescape( name_value.slice(0, equals_pos) ).trim(),
        value      = unescape( name_value.slice(equals_pos + 1) );

    cookies[":" + name] = value;
  }
  return cookies;
}

one that searches through this object and finds the first value that starts with a certain search word:

function findCookieByName(searchWord) {
  var cookies = decodeCookie();

  for (name in cookies) {
    var value = cookies[name];
    if (name.indexOf(":" + searchWord) == 0) {
      return value;
    }
  }
}

So you can do:

var value = findCookieByName("word");

if (value) {
  var pieces = value.split("#"); // > array of values
}

P.S.: I prepend the cookie name with ":" to prevent clashes with built-in properties of objects. For example: You can name your cookie __proto__ but that wouldn't work too well with JavaScript objects. Hence I store all cookie names with a prefix.

Zoran
  • 4,196
  • 2
  • 22
  • 33
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • In findCookieByValue function I think that you are returning cookie name, I need to return cookie value for every cookie which name has "word" in name. – tanasi Sep 23 '11 at 08:37
  • @Ivan: No, I'm returning the value… it even says `return value`. ;-) Why do you think it returns the name? – Tomalak Sep 23 '11 at 09:00
  • I am not good with javascript syntax, I am more into C# so I make mistakes :) I am now trying code, and I ask that to be sure that we understand each other what I need from code :) – tanasi Sep 23 '11 at 09:09
  • @Ivan: Just use your favorite browser's JavaScript debugger to step through the code and see the data structures this creates. – Tomalak Sep 23 '11 at 09:14
  • value.indexOf(searchWord) - "word" is in cookie name, in cookie value data is separated with #. And I need to go through all cookies with "word" in name, and from all this cookies values to take 4 variables. Sorry if I make mistakes with code reading. – tanasi Sep 23 '11 at 09:14
  • 1
    didn't work for me `Uncaught ReferenceError: cookie is not defined ` – Orlo Mar 10 '14 at 10:26
  • Well, it there is no cookie defined then there is no cookie defined. See http://stackoverflow.com/a/17508321/18771. You could change the function to deal with undefined values. – Tomalak Mar 10 '14 at 10:39
  • 1
    @Orlo updated answer to fix the `ReferenceError` - there was a typo in *decodeCookie()*'s for loop which was referencing `cookie` instead of `cookieParts`. – Zoran Dec 17 '15 at 18:36
  • Thanks a lot, @Zoran. – Tomalak Dec 17 '15 at 18:52