February 26, 2021
Hot Topics:

10 Cool Things You Can Do with JavaScript and YUI

  • By Diona Kidd
  • Send Email »
  • More Articles »

1. The YUI

The Yahoo User Interface (YUI) library is a collection of JavaScript and CSS resources that make it easy to build a wide variety of interactive web pages. It also includes easy-to-use widgets that can be directly dropped into a page.

YUI not only handles the cross-browser challenges for developers, from JavaScript to font sizes, but it also includes CSS that can be included to minimize the time to implement a widget. In addition to JavaScript and CSS, the library also includes test scripts, examples, and documentation for YUI components. Using YUI, you can minimize your effort to create a feature rich web interface.

2. Manipulating the DOM

Creating interactive interfaces on the web requires interacting with the Document Object Model. It's okay if you don't have the innermost knowledge of the DOM because YUI does a lot of this work for you in one of its core libraries.

The YUI DOM class offers convenient methods to access DOM elements. This library is a singleton and doesn't require instantiation. Simply put, just include the prerequisite yahoo.js and the library, dom.js, and call the methods directly.

You can do a lot using the DOM Collection library, including easily accessing page elements for manipulation. You also can get and set styles, the XY coordinate of an element, manipulate class names, and even get the Viewport size.

The DOM library is probably most often used to retrieve page elements by either ID or class for manipulation. You also can shorten your code by assigning this singleton object to a variable at the beginning of a function or class. You can do this with almost all of the YUI utility classes.

$D = YAHOO.util.Dom;

This allows you to call any method of the DOM class using a much shorter line of code throughout your function or class.


3. AJAX Requests

There are a lot of AJAX libraries these days. YUI's AJAX implementation has a lighter footprint than some other libraries. The Connection Manager class handles AJAX requests in YUI. It provides a simple but powerful interface while handling cross-browser instantiation of the XMLHttpRequest, handles' states, and server responses.

To use the Connection Manager, include the prerequisites Yahoo and Custom Event, and then the Connection Manager.

<script src="="../yui/build/yahoo/yahoo.js"></script>
<script src="="../yui/build/event/event.js"></script>
<script src="="../yui/build/connection/connection.js"></script>

When you want to handle a response from an AJAX call, create a callback object to handle the server response and the returned data. If you don't care what information comes back from the server, you can omit the callback object.

var callback =
   success: function(o) {/*success handler code*/},
   failure: function(o) {/*failure handler code*/},

The response object is passed into each function in your callback object. If you need to pass in additional objects, numbers, strings, or arrays to your callback methods, you can do this by using the optional argument member.

Each of these members is optional. The Connection Manager will automatically call either success or response member functions depending on the server response. Each of these members is optional because you are overriding the callback object in the Connection Manager and passing in a custom methods for handling the response. If there is no response method defined in your code, nothing happens.

To make an AJAX request, you also need to define the data you want to send to the server, the URL to send the data to, and the method to use. In this example, you'll use the following URL, URL param string, and the HTTP POST method. If the data string has special characters, you would need to encode the postData string.

var sUrl     = 'http://myserver.com/ajax_url';
var postData = 'username=anonymous&password=blank';
var myMethod = 'POST';

At this point, you have defined methods defined to handle success and failure, your designation URL, your POST data string, and your HTTP method. Now, you just need to make your AJAX request using the Connection Manager. Use the asyncRequest method of the Connection Manager to query the server.

var request = YAHOO.util.Connect.asyncRequest(myMethod, sUrl,
   callback, postData);
You also could retrieve a form element from the page by using the DOM class and pass the form object in to the Connection Manager.

4. Drag and Drop

Drag and drop is a cool effect that interacts with the DOM of a web page. It's been gaining popularity over the last few years, cropping up in shopping carts and other interactive interfaces.

Creating drag and drop interaction is easy using the DragDrop library in YUI. To create this effect in YUI, first include the Yahoo DOM Event and the DragDrop classes in the head of your HTML document.

<script type="text/javascript" src="../yui/build/yahoo-dom-event/
   yahoo-dom-event.js" ></script>
<script type="text/javascript" src="../yui/build/dragdrop/
   dragdrop-min.js" ></script>

Then, add an element to the page that you want to drag and drop.

<div id="element1">
Drag &amp; drop this element around the page.

Finally, instantiate the DragDrop object.

var dd1 = new YAHOO.util.DD("element1");

Page 1 of 4

This article was originally published on October 25, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date