How to open external links in a new window in Drupal

Posted by: 
Dominique De Cooman

Create a file containing this piece of javascript.

if (Drupal.jsEnabled) {
 $(document).ready(function() {
  $("a[@href^=http]").each(function() {
    if(this.href.indexOf(location.hostname) == -1) {
      $(this).click(function(){window.open(this.href);return false;});
    }
  });
 });
}

Next in your phptemplate_variables function located in the template php you add the code in this example function.
<?php
function _phptemplate_variables($hook, $vars) {
  if ($hook == 'page') {
        drupal_add_js(path_to_theme().'/js/scripts.js');
        $vars['scripts'] = drupal_get_js();
    return $vars;
  }
  return array();
}
?>

Add new comment