is it possible to call a javascript(jquery) function once a particular page is loaded. It should be hacking the of the whole site and to detect what the current page is and call the function. How can I actually do this?

So in a non-drupal php site, I would add a js file and have a start_product_content() function there. In the head.php file it would be something like this:

    <?php if $current_page = "product" ?>

I have explained in a different post about what I really want to achieve, long story: footer link to a page with hash changed content ready

I have achieved this when I was doing a non-drupal website, now I just want to be able to do this so that I can be closer to the target.


  • 1
    Drupal is a CMS framework that produces HTML/CSS/JS etc. What works in a non-Drupal website will also work in a Drupal website. Presumably you've already tried to implement this, what went wrong? What error messages did you receive? What code did you use to add the JS to the page? Why aren't use using jQuery.ready() as was advised in the linked post? All these things needs to be known to help any further. If you're just having a problem with the Javascript syntax I'm afraid this isn't the right place to ask; we deal with Drupal-specific questions only here
    – Clive
    Sep 12, 2013 at 12:20
  • I simply don't know how to add the equivalent php code into a drupal site, so I haven't tried anything because I don't know where to start. How can I call a function right in the beginning once detecting the page? Sep 12, 2013 at 12:22
  • I'll put an answer in for that specific question, but you're dangerously close to asking "how does Drupal work?" or "How does Drupal's theme system work?", both of which are scarily large topics that we can't cover here. Have you read the docs for theming for example?
    – Clive
    Sep 12, 2013 at 12:23
  • Maybe I am not clear enough, sorry. I am not asking how to add javascript files, I have done that. I am asking, how to call a js function from the file that I have added when the browser has detected the $path_alias is some certain strings that I will specify or, even better, when a particular node type (the pages) is loaded. Sep 12, 2013 at 12:29
  • That's not the way to approach it - The conditional should be on the PHP side. Wherever you're adding the JS to the page should contain a conditional of something like if ($_GET['q'] == 'node/1') { drupal_add_js(...); } So the javascript simply doesn't get added to pages it shouldn't run for. Then you can just make sure your JS is running when the page is loaded and you're done
    – Clive
    Sep 12, 2013 at 12:32

2 Answers 2


If you want to add arbitrary code to the <head> element you need to:

  1. Take a copy of the core html.tpl.php file and put it in your theme somewhere
  2. Clear the caches so it gets added to the theme registry
  3. Implement hook_preprocess_html() in your theme's template.php file like so

    function MYTHEME_preprocess_html(&$vars) {
      $vars['some_var'] = 'Something';
  4. In the html.tpl.php file, use

    print $some_var;

    wherever that might be appropriate.

If you want to add structured code to the <head> tag then use drupal_add_html_head().

Finally, specifically to add JS to the page, you can use drupal_add_js(). But that needs to happen before the HTML template is processed. hook_preprocess_page() would be one option, but there are many, depending on the context that makes up your page.

You'll find the Theming guide, and questions already asked on this site, invaluable if you're learning the theme system from scratch.


Drupal has is own way to add a specific .js file :

Use drupal_add_js()

There also a module (with an UI) to add .js file without creating a custom module. Take a look at JS injector

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.