Adding Javascript to Drupal

Using PHP

The function drupal_add_js() lets you add a reference to a file, as well as inline javascript code:

drupal_add_js('misc/myscript.js');
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');

See the function reference page for additional examples and settings.

On a Theme

If found, Drupal automatically includes the file called script.js in the theme directory.

You can also add a js file to the .INFO file using this line:


scripts[] = myscript.js