Drupal – Load External Javascript using drupal_add_js() @ 1

If you want to include external .js file in your custom module, create the following file.
external.js

(function ($) {
  Drupal.behaviors.myModule = function(context) {
    var externalScript = $('<script></script>').attr('type','text/javascript').attr('src', 'http://example.org/example.js');
    $('body').append(externalScript);
  }
}(jQuery));


 

Then whenever you want to add the http://example.org/example.js, add it by drupal_add_js().

drupal_add_js(drupal_get_path('module', '<module>') . '/external.js', 'module');

 

Done =)

Next: Drupal – Load External Javascript using drupal_add_js() @ 2

Reference: Including external javascript files in drupal 6

One thought on “Drupal – Load External Javascript using drupal_add_js() @ 1”

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.