YUI Library Home

YUI Library Examples: Cookie Utility: Simple Cookie Example

Cookie Utility: Simple Cookie Example

This example shows basic usage of the YUI Cookie utility. The example checks the value of a cookie and then sets it to a new value.

Note:

Keep an eye on the Logger console at right to view the log messages associated with this example.

Simple Cookie Example

This example begins by getting the value of a cookie named "example". If this is the first time you've run the example, the value should be null:

1var currentValue = YAHOO.util.Cookie.get("example"); 
view plain | print | ?

This value is output into the Logger. Next, the cooke is set to a random value:

1var newValue = "yui" + Math.round(Math.random() * Math.PI); 
2YAHOO.util.Cookie.set("example", newValue); 
view plain | print | ?

When you reload the page, the value of the cookie should be the one that was just set.

Note: this example uses session cookies, so the value will be lost if you close the browser.

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

YUI Logger Output:

Logger Console

INFO 16ms (+0) 1:20:06 PM:

global

Setting cookie's value to 'yui1852'

INFO 16ms (+16) 1:20:06 PM:

global

Cookie's current value is 'null'

INFO 0ms (+0) 1:20:06 PM:

global

Logger initialized

More Cookie Utility Resources:

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings