Making cookies with Javascript

new Cookie({eggs: 1, flour: 3, sugar: 1.5, brownSugar: 1});

Oh, wait…not those kind of cookies (mmm, now I’m hungry for cookies).

The script.aculo.us wiki has some code for working with cookies in JavaScript. I’ve extended it a bit to allow for other options when setting the cookies. Here’s how to use it:

// setting cookies
Cookie.set('name', 'value');

// change domain, path, and expiration in # of days
Cookie.set('name', 'value', {
  domain: 'foobar.com',
  path: '/path',
  expires: 14
});

// the google cookie (doesn't expire)
Cookie.set('name', 'google', {expires: false});

// reading cookies
Cookie.get('name');

// Get an array all cookies that are set
Cookie.all();

// erase a cookie
Cookie.erase('name');

// check if browser accepts cookies
if(Cookie.accept()) {
  // do stuff with cookies
}

You can grab the code from here.

posted by brandon | updated July 11th 04:24 AM

Speak your mind:

(Required)

(Required)


(You may use textile in your comments.)

About

I'm Brandon Keepers, a web application developer that likes beautiful code, valid markup and adherence to standards. As a part of Collective Idea in Holland, Michigan, I practice Agile software development primarily using Ruby on Rails.

-86.103171 42.785037

Contact:

more »

Syndicate