Including external javascript files in drupal 6

firebus's picture

In D7, drupal_add_js can handle external javascript files, but in D6 it cannot handle external js in a nice way.

Most people instead include external js with a preprocess_page function, or hook_footer. However, in both cases, the external js is added before the scripts added by drupal_add_js (the $scripts page template variable) which can be a performance problem.

I've seen one kind of nasty workaround that (ab)uses drupal_add_js to make a call to document.write:

Here's another technique to include an external script with drupal_add_js in Drupal 6 that I find a little more readable.

Create a local js file (in your module or theme) and include that with drupal_add_js:

drupal_add_js(drupal_get_path('module', 'my_module') . '/my_module.js', 'module');

In your local js file, create a new script element and append it to the body:

(function ($) {
  Drupal.behaviors.myModule = function(context) {
    var externalScript = $('<script></script>').attr('type','text/javascript').attr('src', '');



The Advanced CSS/JS Aggregation module handles external js in a nice way. Check it out! This works in D6 if you have advagg enabled.

drupal_add_js('', 'external');

firebus's picture


advagg ended up being too heavy for my use case - i didn't want a replacement for built-in css/js aggregation, i just wanted a way to include external js.

after scanning advagg's code to see how *they* were handling external files, i was hopeful that i could continue to use drupal_add_js('url', 'external') and write my own function to build external links.

so to that effect, i
- added a variable $external_scripts in my helper module preprocess_page
- wrote a function to output correct tags from the external elements of the javascript array returned by drupal_add_js()
- output $external_scripts in my theme's page.tpl.php

that didn't quite work - i got my external scripts happily from $external_scripts, but i got the external scripts a second time in the $scripts variable, as if they were local scripts (so they all returned 404s).

this is because drupal_get_js() treats 'core', 'module', and 'theme' types as a default case - 'external' gets lumped in there too and treated like a local script.

i'm not sure how advagg gets around this - i didn't have the patience to really figure it out.

so instead of using drupal_add_js('url', 'external') to add my js, i'm just building a local array of urls and then building an $external_scripts variable from that.

this is not ideal - especially, there is no static external javascript array that other custom modules can use. but in the absence of a good backport of an external javascript type from d7 (have you read that node? this should have been in d5! the community really shows it's fail with issues like these) this is a reasonable light-weight solution for me.

firebus's picture


That is lovely, thanks so much for the tip! I will give it a try.

Powered by Drupal - Design by Artinet - Amazon Affiliate