Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to get a cookie by name in JavaScript?

I am using prototype and I can't find any built in extensions to set or retrieve cookies. After googling for a little bit, I see a few different ways to go about it. I was wondering what you think is the best approach for getting a cookie in JavaScript?

like image 743
EvilSyn Avatar asked Sep 25 '08 20:09

EvilSyn


1 Answers

I use this routine:

function ReadCookie(name)
{
  name += '=';
  var parts = document.cookie.split(/;\s*/);
  for (var i = 0; i < parts.length; i++)
  {
    var part = parts[i];
    if (part.indexOf(name) == 0)
      return part.substring(name.length)
  }
  return null;
}

Works quite well.

like image 148
PhiLho Avatar answered Oct 30 '22 18:10

PhiLho