8

I have a block of JavaScript I need to add to my homepage (and only my homepage) to enable the logging of clicks via a heat mapping service I signed up for.

They specified the code needed to be placed just before the </body> tag, but I imagine it's safe to put it anywhere within the <body> tag.

I'd like the snippet not to appear on any other pages. How can I accomplish this?

I am running Drupal 6.

3 Answers 3

15

If by home page you mean the front page, then you can implement hook_preprocess_page() in a custom module.

function mymodule_preprocess_page(&$variables) {
  if ($variables['is_front']) {
    drupal_add_js(drupal_get_path('module', 'mymodule') . '/mymodule.js');
    $variables['scripts'] = drupal_get_js();
  }
}

The same code works on Drupal 7, as $variables['is_front'] is set in _template_preprocess_default_variables() (called from template_preprocess()), using the following code.

  // drupal_is_front_page() might throw an exception.
  try {
    $variables['is_front'] = drupal_is_front_page();
  }
  catch (Exception $e) {
    // If the database is not yet available, set default values for these
    // variables.
    $variables['is_front'] = FALSE;
    $variables['db_is_active'] = FALSE;
  }

The reason of calling drupal_add_js() first, and then drupal_get_js() is that, in the case another module that is executed after would run the following code, the JavaScript file would still be added.

function quite_a_different_module_preprocess_page(&$vars) {
  drupal_add_js(drupal_get_path('module', 'quite_a_different_mymodule') . '/quite_a_different_module.js');
  $vars['scripts'] = drupal_get_js();
}

Using drupal_add_js(), the file will be added to the output returned from drupal_get_js().

4
  • This looks good, will try it out.
    – PHLAK
    Jun 6, 2011 at 15:23
  • This works well for including the JS in the header, but is there a way to put it in the body?
    – PHLAK
    Jun 6, 2011 at 15:55
  • 1
    @PHLAK To put it in the body, you just add a <script> tag in the HTML body.
    – apaderno
    Jun 6, 2011 at 15:58
  • I was able to accomplish what I needed by adding a content block at the end of the page and limit it to only show on the <front> page. This solution is also good, but not for the situation I was in. Thanks.
    – PHLAK
    Jun 6, 2011 at 16:28
6

If the code can go in the head section, then add the following function in your template.php.

function MYTHEME_preprocess_page (&$vars) {
  if ($vars['is_front']) {
    $vars['scripts'] .= "// my JS for the home page";
  }
}

You can also add the following code, in the appropriate place of your page.tpl.php.

<?php if ($is_front): ?>
  // JS goes here
<?php endif; ?>

There are other ways to accomplish this, but this is probably the easiest.

-3

In Drupal 7 if you want to include the js in the tag with the

last solution, the file you have to modify, after you copied it inside your theme from the system folder, is html.tpl.php.

Not strictly the Drupal way but works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.