Dominique De Cooman
Published on Dominique De Cooman (https://dominiquedecooman.com)

Home > How to open external links in a new window in Drupal

Dominique De Cooman
Wednesday, August 20, 2008 - 10:47

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();
}
?>


Source URL: https://dominiquedecooman.com/blog/how-open-external-links-new-window-drupal