Drupal - Add external javascripts

In your theme's template.php file use drupal_add_js() to the page preprocess method (or an equivalent preprocess).

function hook_preprocess_page(&$vars) {
  drupal_add_js('http://cdn.jquerytools.org/1.2.6/jquery.tools.min.js', 'external');
}

The method of adding javascript depends on for what you are adding javascript for. There are different ways to add js.

  • By defining in the theme.info file.

  • Adding it inline.

  • Adding an external js file.

You can add a js info file with this simple line of code

scripts[] = js/myscript.js

Or you can use the php function provided by drupal drupal_add_js

This functions lets you easily add a JavaScript file or helps in setting an inline code to any page in your theme and it takes 5 parameters (you see it in the api reference).

There is an detail post on this here.

Tags:

Javascript

7