2

I wanted to add custom JavaScript code on certain nodes. I have written this code in the html.tpl.php template, but this doesn't work. (donate is the path alias for a node.)

<?php if(arg(0) == 'DONATE'): ?>
  <script>
    (function() {
      // ...
    );
  </script>
<?php endif; ?>
3
  • then what's the problem just put your js inside script tag.
    – Nishant
    Commented May 29, 2015 at 7:08
  • did you read question? :) I want that script only on specific page.
    – shrish
    Commented May 29, 2015 at 7:10
  • in your situation arg(0) will be 'node' and arg(1) will be ID of a node
    – Andrew
    Commented May 29, 2015 at 7:17

4 Answers 4

8

You could do this:

<?php
$node = menu_get_object();
if ($node && $node->nid == DONATE_NODE_ID):
?>
<script></script>
<?php endif; ?>

But a better way would be to:

  1. Wrap your script in Drupal.behaviors:

    (function ($) {
      Drupal.behaviors.NameOfYourTheme = {
        attach: function (context, settings) {
          // Your JS code goes here.
        }
      }
    })(jQuery);
    
  2. Call drupal_add_js() in hook_preprocess_page(). This code would go to template.php:

    function YOUR_THEME_preprocess_page(&$variables) {
      $node = menu_get_object();
      if ($node && $node->nid == DONATE_NODE_ID) {
        drupal_add_js(drupal_get_path('theme', 'YOUR_THEME') . '/path/to/js.js');
      }
    }
    
2
  • Okay the first did work, since I wanted it on header. not sure if we can do same with drupal_add_js function. Thanks much.
    – shrish
    Commented May 29, 2015 at 7:26
  • 1
    Yep, drupal_add_js() will append the script to the list of scripts in header. Commented May 29, 2015 at 7:27
1

Use the following code in your theme's template file

function mythemename_preprocess_page(&$vars, $hook) {

 $node = menu_get_object();
 if ($node && $node->nid == "node id of this node here"):
    drupal_add_js(path_to_theme().'/js/events.js');
  }

  $vars['scripts'] = drupal_get_js();
}
1

I like this solution because you can use path aliases w/ wildcards. https://gist.github.com/bjmiller121/3d6cf18ceccc8de5c057

Note: this solution uses aliases so it probably only makes sense to use it when URLs are very predictable and do not change.

In your theme's template.php (or custom module)

/**
 * Implements hook_preprocess_html
 * @param $variables
 */
function themename_preprocess_html(&$variables) {

  $alias = drupal_get_path_alias(current_path());
  $urls = array(
    'example/url',
    'example/url/*',
  );
  $patterns = implode("\n", $urls);

  if (drupal_match_path($alias, $patterns)) {
    // Do something only on ther specified URL patterns
  }

}
0

In your html.tpl.php file, use below code to implement javascript on your node.

if(arg(0) == 'node') {
        $arr_list_of_nodes = array('3', '4', '7');
        $nid = arg(1);

        if(in_array($nid, $arr_list_of_nodes)) {
                ?><script type=""text/javascript"" language=""Javascript"">
                        //your js code
                </script><?php
        }
}

Hope this helps you.

Thanks!

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.