Add Javascript to a Specific Page
There are several ways to add javascript code to a page in Drupal. The method outlined below involves a modification to your theme’s template.php file. You’ll be editing (and un-commenting if necessary) your theme’s preprocess_page function. This basically lets you modify the variables that are available in your page.tpl.php file(s).
In the example below, I’ve checked if the page’s “is_front” property is true. You can use anything available in the $vars variable within the function. I suggest using the devel module to determine what variables are available. This can be done with: dpm($vars);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * Override or insert variables into the page templates. * * @param $vars * An array of variables to pass to the theme template. * @param $hook * The name of the template being rendered ("page" in this case.) */ function themename_preprocess_page(&$vars, $hook) { if ($vars['is_front']) { drupal_add_js(path_to_theme() . '/_javascript/homepage_rotate_bg.js', 'core'); } $vars['scripts'] = drupal_get_js(); } |