06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

[ 91 ]<br />

Chapter 3<br />

$(document) creates a <strong>jQuery</strong> object that wraps the document object. The ready()<br />

function assigns an event h<strong>and</strong>ler to the ready event of that object. This ready<br />

event is a special <strong>jQuery</strong> event that fires when the DOM is ready for manipulation.<br />

Roughly, though, it functions like the (on)load event, <strong>and</strong> should almost always be<br />

used instead of an onload h<strong>and</strong>ler.<br />

The ready() function takes a function as a parameter. Often, developers will write<br />

an inline anonymous function to do the initialization. But we've already created an<br />

init() function that is suitable for our needs. So all we have to do is pass in that<br />

function object, <strong>and</strong> <strong>jQuery</strong> takes care of the rest.<br />

But what if we have multiple scripts that need to do something when the document<br />

is ready? Do we need to write a master function to h<strong>and</strong>le all of the initialization?<br />

No we do not. One of the nice things about <strong>jQuery</strong>'s event model design is that<br />

we can register multiple h<strong>and</strong>lers for the ready event (or any other event)—<strong>and</strong><br />

we can even do this in different script files. So, we can use $(document).ready()<br />

multiple times.<br />

Now we have a tool that will rotate through sticky nodes, displaying them in order.<br />

Using the fade effect provided by <strong>jQuery</strong>, it will smoothly transition from one sticky<br />

node to the next. It will ac<strong>com</strong>plish our goals of keeping the important information<br />

right at the top, while not occupying too much space (<strong>and</strong> subsequently pushing<br />

other information further down the page).<br />

More importantly, we've gotten to see <strong>jQuery</strong> in action, providing querying tools,<br />

DOM manipulation features, effects, <strong>and</strong> event h<strong>and</strong>lers. This will lay groundwork<br />

for later chapters.<br />

A brief look backward<br />

Now that we've seen the possibilities that <strong>com</strong>e with <strong>jQuery</strong>, let's briefly reconsider<br />

how we built the printer tool in Chapter 2.<br />

To build that tool, we had to make changes within the HTML of our theme, <strong>and</strong> we<br />

had to work with various DOM methods such as getElementById().<br />

With <strong>jQuery</strong>, we could have skipped the template part altogether, using the ready()<br />

function (<strong>and</strong> other <strong>jQuery</strong> event h<strong>and</strong>lers) to create the printing link as the page was<br />

created. In fact, we will do something like this in the <strong>com</strong>ing chapter.<br />

Likewise, we could probably have trimmed down the size of our script by making<br />

use of <strong>jQuery</strong>'s chaining feature.<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!