So far, we have seen just a few lines of jQuery code. But, our examples have been independent of Drupal. How can we use jQuery inside of Drupal?
The answer is simple. If we write our JavaScript "the Drupal way", then Drupal will make it very easy to use jQuery. Here's what I mean.
How do we include JavaScript files in Drupal themes? By using the scripts[]
directive in the theme's .info
file. And if you're a PHP programmer, you can use the drupal_add_js()
function in either a theme or a module.
If you use either of these means, then Drupal will automatically include the basic JavaScript libraries, including drupal.js
and jquery.js
. That's right. Drupal not only includes jQuery in its core distribution, but automatically includes it for you.
To see an example of this, we need not look further than the code we wrote in the previous chapter. Looking in the<head></head>
section of the HTML for our theme, we can see the following<script></script>
tags...