(Tutorial) Add JavaScript to Drupal 6 for the front page only

DRUPALRANCH Newsletter:

Tips : Add JavaScript to Drupal 6 for the front page only

his cropped up in the Drupal forums today, again, so I dug this bit of code out since I was messing about with recently. If you want to load a .js file only for the homepage there's a couple of ways of doing it.

First in template.php you can simply add the following - which assumes you have saved the .js file in your theme folder. Adjust the path to suit (for example /js/myscript.js).

if (drupal_is_front_page()) {
  drupal_add_js(drupal_get_path('theme', 'mytheme').'/myscript.js', 'theme');
}

This is nice since you can add a few more parameters to it - see the full story here http://api.drupal.org/api/function/drupal_add_js

| Read more

Courtesy : Adaptivethemes.com



Tag Cloud