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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ 255 ]<br />

Chapter 8<br />

3. To add an individual setting to the <strong>Drupal</strong>.settings array. We have seen<br />

the <strong>Drupal</strong>.settings.basePath variable in previous chapters. This extends<br />

that same object.<br />

Unfortunately, the arguments are different in each case <strong>and</strong> therefore mean different<br />

things. Here, we have used the simplest case as we are simply adding a file. The<br />

syntax for doing this from within a module is drupal_add_js($filename). As we<br />

can see, the drupal_add_js() function needs to know the path to the filename.<br />

In the above code snippet, this loads each script file.<br />

In the next project, we will see how drupal_add_js() can be used to add settings.<br />

The three uses of this function are documented in the <strong>Drupal</strong> API at<br />

http://api.drupal.org/api/function/drupal_add_js/6.<br />

That is all there is to our loader. Now, every script that we add to the .info file using<br />

the scripts[] directive will automatically be loaded on every page request. At the<br />

beginning of this project, we created a module that fired an alert() every time a page<br />

loaded. Now that our module is <strong>com</strong>plete, we should be able to test the loader by<br />

loading a page on our site. Here's a screenshot of what that should look like:<br />

If the alert box pops up, we know that our script is being loaded.<br />

The module we just created provides a way for integrating <strong>JavaScript</strong> files into<br />

<strong>Drupal</strong>, without relying on a theme, <strong>and</strong> without requiring any additional code.<br />

With this single module, we could add all of the scripts created since Chapter 3. The<br />

one in Chapter 2 relied a little too heavily on a template file to be used outside of a<br />

theme. No additional PHP would be necessary.<br />

On the other h<strong>and</strong>, this module doesn't exploit the possibility of using PHP code<br />

to inform <strong>JavaScript</strong>. In the next project, we will see how we can use PHP code in<br />

modules to pass information on to a <strong>JavaScript</strong> tool.<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!