INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Cookies

How to Read and Write multiple Cookie variables by tlhawkins
Posted: 22 May 02

I've noticed many people asking questions about how to do a particular job with cookies.  I've found these 2 functions very helpfull and I wanted to pass them on.

I adapted one of these from a book on javascript and I made the other one myself.  I hope they work for you.
The idea is to be able to write more then one variable to a cookie and then retrieve them.  I added the option of picking your own delimiter for multiple cookie variables because sometimes you may want to save text that has a common delimiter, or you may want to make a 3 demensional array in a cookie by changing the delimiter.

So here are the functions:


function readCookie(name,delimit)
{
  if (document.cookie == '')
  {
    return false;
  }
  else
  {
    var fC,lC;
    var mcookie = unescape(document.cookie);
    fC = mcookie.indexOf(name);
    var ph = fC + name.length;
    if ((fC != -1) && (mcookie.charAt(ph) == '='))
    {
      fC += name.length + 1;
      lC = mcookie.indexOf(delimit,fC);
      if (lC == -1) lC = mcookie.length;
      return unescape(mcookie.substring(fC,lC));
    }
    else
    {
      return false;
    }
  }
}

function returns false if there was no cookie found with that name.

Here is the write cookie function.  It's a little more complicated.


function writeCookie(name,value,delimit)
{
  if (document.cookie == '')
  {
    document.cookie = escape( name + "=" + value );
    return false;
  }
  else
  {
    var B4,Af,fC,lC;
    var mcookie = unescape(document.cookie);
    var ncookie;
    fC = mcookie.indexOf(name);
    var ph = fC + name.length;
    if ((fC != -1) && (mcookie.charAt(ph) == '='))
    {
      B4 = mcookie.substring(0,fC);
      fC += name.length + 1;
      lC = mcookie.indexOf(delimit,fC);
      if (lC == -1) lC = mcookie.length;
      if (lC == mcookie.length)
      {
        ncookie = B4 + name + "=" + value;
        document.cookie = escape(ncookie);
        return true;
      }
      else
      {    
        Af = mcookie.substring(lC,mcookie.length);
        ncookie = B4 + name + "=" + value + Af;
        document.cookie = escape(ncookie);
        return true;
      }    
    }
    else
    {
      ncookie = mcookie + delimit + name + "=" + value;
      document.cookie = escape(ncookie);
      return false;
    }
  }
}    


I hope these prove usefull to you.
If anyone has improvements on these scripts I would love to hear them.



Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close