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.

In this case, we have a string object, not a <strong>jQuery</strong> object. Sure, we could continue<br />

chaining, but we could only call functions of the string object. For instance, the<br />

following would work just fine:<br />

$('h1').text().substring(0,1);<br />

The text() function returns a string, <strong>and</strong> substring() is a member function of<br />

a string object. However, we could not do this:<br />

$('h1').text().css('color','red');<br />

[ 71 ]<br />

Chapter 3<br />

This would generate an error looking something like this: TypeError: $("h1").text().<br />

css is not a function. That happens because a string object does not have a<br />

css() function.<br />

At this point, the important things to underst<strong>and</strong> about <strong>jQuery</strong> are:<br />

•<br />

How queries can be <strong>com</strong>posed from CSS selectors<br />

• How $() <strong>and</strong> <strong>jQuery</strong>() return <strong>jQuery</strong> objects (that often wrap lists of<br />

elements from the document)<br />

•<br />

That <strong>jQuery</strong> function calls are often chained together to produce longer lines<br />

that do more<br />

We will pick up more about <strong>jQuery</strong> as we proceed.<br />

Feeling <strong>com</strong>fortable with the basic concepts of <strong>jQuery</strong>? Now it's time to get back<br />

to <strong>Drupal</strong>. We will be making use of <strong>jQuery</strong> to build <strong>Drupal</strong>-centric <strong>JavaScript</strong>.<br />

Using <strong>jQuery</strong> in <strong>Drupal</strong><br />

So far, we have seen just a few lines of <strong>jQuery</strong> code. But, our examples have been<br />

independent of <strong>Drupal</strong>. How can we use <strong>jQuery</strong> inside of <strong>Drupal</strong>?<br />

The answer is simple. If we write our <strong>JavaScript</strong> "the <strong>Drupal</strong> way", then <strong>Drupal</strong> will<br />

make it very easy to use <strong>jQuery</strong>. Here's what I mean.<br />

How do we include <strong>JavaScript</strong> files in <strong>Drupal</strong> themes? By using the scripts[]<br />

directive in the theme's .info file. And if you're a PHP programmer, you can use<br />

the drupal_add_js() function in either a theme or a module.<br />

If you use either of these means, then <strong>Drupal</strong> will automatically include the basic<br />

<strong>JavaScript</strong> libraries, including drupal.js <strong>and</strong> jquery.js. That's right. <strong>Drupal</strong> not<br />

only includes <strong>jQuery</strong> in its core distribution, but automatically includes it for you.<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!