Book Image

Yahoo User Interface Library 2.x Cookbook

By : Matt Snider
Book Image

Yahoo User Interface Library 2.x Cookbook

By: Matt Snider

Overview of this book

<p>The Yahoo! User Interface (YUI) Library is a set of utilities and controls, written in JavaScript, for building richly interactive web applications using techniques such as DOM scripting, DHTML, and AJAX. Although you can create stylish Internet applications by modifying its default components, even advanced users find it challenging to create impressive feature-rich Internet applications using YUI.</p> <p>This book will help you learn how to use YUI 2.x to build richer, more interactive web applications that impress clients and wow your friends. It has recipes explaining over twenty-five YUI components, showing how to use them, and how to configure them to meet your needs. Each covered component will have extractable code samples that showcase the common ways that the component is used.</p> <p>The book starts by explaining the core features of YUI 2.x, the utilities that the rest of the library depends on and that will make your life easier. It then explains how to build UI components and make AJAX requests using the YUI framework. Each recipe will cover the most common ways to use a component, how to configure it, and then explain any other features that may be available. We wrap things up by looking at some of the recent beta components and explain how to use them, and how they may be useful on your web application.</p> <p>For each of the recipes, there is an introductory example, then more advanced examples, followed by an explanation of how the component works and what YUI is doing. For more experienced developers, most recipes also include additional discussion of the solution, explaining to further customize and enhance the component.</p>
Table of Contents (22 chapters)
Yahoo! User Interface 2.x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using the Cookie component


Cookies are a powerful way to store variables between user visits. YUI has a set of static functions that simplifies the reading and writing of cookies. Additionally, you can use it to write fewer cookies by storing multiple values in a single cookie. This recipe will explain how to use the static functions of the Cookie component, available on the YAHOO.util.Cookie namespace.

How to do it...

The simplest way to add a cookie that expires at the end of the current user session is:

YAHOO.util.Cookie.set('testCookie1', 'testValue');

You can provide a third optional object literal argument to define any of these additional cookie properties:

YAHOO.util.Cookie.set('testCookie2', 'testValue', {
expires: new Date("January 1, 2012"), // last until date
domain: 'myserver.com', // bind to your server
path: '/', // bind to a path on the server
secure: true // use SSL
});

Use the following to read a cookie:

var value = YAHOO.util.Cookie.get("testCookie1");

To convert the cookie to another basic data type, such as a Number, use:

var value = YAHOO.util.Cookie.get("aNumberCookie", Number);

To convert the cookie to another data type manually, use:

var value = YAHOO.util.Cookie.get("aHexCookie", function(stringValue) {
return parseInt(stringValue, 16); // hexadecimal value
});

When deleting a cookie you must specify the same options as were used when creating the cookie (except expires). Here is how to delete the two cookies we previously created:

YAHOO.util.Cookie.remove('testCookie');
YAHOO.util.Cookie.remove('testCookie2', {
domain: 'myserver.com',
path:'/',
secure: true
});

To check for the existence of a cookie, use:

var cookieName = 'testCookie1';
if (YAHOO.util.Cookie.exists(cookieName)) {
alert('Cookie "' + cookieName + '" exists');
}

To reduce the number of cookies used by your site, the YUI Cookie component provides a subcookie framework, which stores multiple pieces of data into a single cookie. First create the configuration options that will be used for the subcookies:

var cookieOptions = {
domain: "yourserver.com"
};
var cookieName = 'testCookie';

To set a subcookie use:

var subCookieName = 'count';
var subCookieValue = 22;
YAHOO.util.Cookie.setSub( cookieName, subCookieName, subCookieValue, cookieOptions);

To set many subcookies at one time use:

var testCookiesObject = {
count: 22,
name: 'matt',
isUser: true
};
YAHOO.util.Cookie.setSubs( cookieName, testCookiesObject, cookieOptions);

To fetch a subcookie, use:

var value = YAHOO.util.Cookie.getSub(cookieName, 'count');

You may also convert subcookies to non-string datatypes by passing a third argument:

var value = YAHOO.util.Cookie.getSub( cookieName, 'count', Number);

To retrieve all subcookies as an object, use:

var oData = YAHOO.util.Cookie.getSubs(cookieName);

And to remove a subcookie use:

YAHOO.util.Cookie.removeSub( cookieName, "count", cookieOptions);

How it works...

The cookie property of document is where browsers store cookies for the JavaScript engine. The YUI Cookie component reads and writes to this value in a simple and concise way, so you don't have to. It is important to remember when creating cookies that if you do not specify the expires property, then the cookie will expire at the end of the user's session. Also, when deleting cookies, the same options that were used to create the cookie should be passed to the remove function or the cookie will not be removed.

There's more...

Subcookies are cookies containing a collection of other name/value pairs. This is accomplished by using a URL parameter query like structure:

cookiename=subcookiename1=value1&subcookiename2=value2&subcookiename3=value3

When a cookie is converted into a subcookie, any existing value will be overwritten by the subcookies infrastructure.

To learn more about cookies and see additional examples, visit http://developer.yahoo.com/yui/cookie/.