I searched four hours on how to do this and could not find the answer.

I want to add some simple java script and functions to my nodes. I want
to call the javascript functions from a file in the section as
it is supposed to be called:

As we know drupal generates the section. I cannot figure out to tell drupal to include my javascript file? I read about the misc/drupal.js functions but these appear to be for module development. Thanks for any help.

Comments

sopko’s picture

I want to add the script src=sites/all/js/my.js tag
to the <head> section in drupal to call my javascript functions.
It is recommended to put your functions in the <head> section

Like this:

<head>

<script type="text/javascript" src=sites/all/js/my.js>
</script>

</head>
coreyp_1’s picture

Perhaps this could help?

- Corey

hagrin’s picture

Just open the folder for the theme you are using and find the file that has your HEAD tags (the default themes have them in the page tpl file I believe). Simply add your code to that file and the JS functions should attach to all your pages.

ceejayoz’s picture

Using the drupal_add_js() function is the proper way of doing this.

sopko’s picture

I am using the contributed newsportal them.

First I tried placing the drupal_add_js('sites/all/js/my.js')
function in the page.tpl.php file. This did not work, it did not produce anything.
Manually putting the script section:

<script type="text/javascript" src=sites/all/js/my.js>

in the page.tpl.php file worked fine.

I then put the drupal_add_js('sites/all/js/my.js'); function in the
template.php file that came with the theme and it aslo worked fine.
That is it put in the proper script tag in the header section. I will
go with this. Thanks.

ceejayoz’s picture

Putting it in page.tpl.php indeed will not work for drupal_add_js. It needs to go in your theme's template.php.

jweedman’s picture

Very simple - and helpful!

exterm’s picture

I am trying to implement the following script below

http://www.dynamicdrive.com/dynamicindex4/stepcarousel.htm

in page can pls help me..

how do i add the script on template.php

function drupal_add_js('sites/all/js/my.js'); is this the right way???

aryashreep’s picture

You can add javascript to head by using theme_preprocess_html()

function theme_preprocess_html(&$var) {
      $script = array(
        '#tag' => 'script',
        '#attributes' => array('type' => 'text/javascript'),
        '#value' => 'Add your javascript here',
       );
       drupal_add_html_head($script, 'script');  
    }