How to add external javascript to my Drupal website?

I want to know how can I add external javascript to a Drupal site

Add Comment
1 Answer(s)
Best answer

Let say you have a script with the following URL – http://cdn.jquerytools.org/1.2.6/jquery.tools.min.js  that you want to add to a theme on the site. To do that, you can use the drupal_add_js() function in the template.php file of the theme in the page preprocess method (or an equivalent preprocess) like this:

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

If you need to pass additional parameters, then the above example will change into :

function hook_preprocess_page(&$vars) {
drupal_add_js(‘cdn.jquerytools.org/1.2.6/jquery.tools.min.js’;, array(‘type’ => ‘external’, ‘scope’ => ‘header’, ‘weight’ => 10));
}

That should do the trick for you.

Kevin Cane Answered on October 21, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.