1

I wanted to add custom javascript on certain pages, in this case it is /DONATE page (example.com/DONATE and it's a node).
I have written this in my html.tpl.php but this doesn't work.

<?php if(arg(0) == 'DONATE'): ?>
    <script>
    (function() {
      // Some custom Javascript.
    );
    </script>
<?php endif; ?>
  • /DONATE page is created custom or by drupal node ? – Nishant May 29 '15 at 6:48
  • it's a node, I have updated my question :) – shrish May 29 '15 at 6:50
  • then what's the problem just put your js inside script tag. – Nishant May 29 '15 at 7:08
  • did you read question? :) I want that script only on specific page. – shrish May 29 '15 at 7:10
  • in your situation arg(0) will be 'node' and arg(1) will be ID of a node – Andrew May 29 '15 at 7:17
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');
      }
    }
    
  • 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 May 29 '15 at 7:26
  • 1
    Yep, drupal_add_js() will append the script to the list of scripts in header. – Aram Boyajyan May 29 '15 at 7:27
  • okay I will try that out as well :) – shrish May 29 '15 at 7:28
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, privacy policy and cookie policy

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