YUI 3.x Home -

YUI Library Examples: Cookie: Simple Cookie Example

Cookie: 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.

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:

  1. var currentValue = Y.Cookie.get("example");
var currentValue = Y.Cookie.get("example");

This value is output in an alert. Next, the cooke is set to a random value:

  1. var newValue = "yui" + Math.round(Math.random() * Math.PI);
  2. Y.Cookie.set("example", newValue);
var newValue = "yui" + Math.round(Math.random() * Math.PI);
Y.Cookie.set("example", newValue);

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.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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