Opening an external link in a new tab

Hello everyone,

I would like to make it possible to open a new tab when you click on an external link of main menu (rather than go directly to the link page, which is the default set-up).

I found the menu target module, but it causes errors for me. Do you have any other suggestion?

Best regards,
Ioannis

Hello Ioanni,

First of all the Menu Target module probably causes errors because it is a Drupal 7 module and you try to install it in a D8 site (I guess).

I cannot find a module online that does the job for you but you can write a custom module and add the following snippet within the .module file:

function MYMODULE_link_alter(&$variables) {
  if ($variables['url']->isExternal()) {
    $variables['options']['attributes'] = ['target' => '_blank'];
  }
}

where MYMODULE is the machine name of your module.

Let me know if there is something that is not clear.

Kostas

Hello again Ioanni,

Apparently @sboutas just informed me that there is a module for that called External Links and it will do the job.

Kostas

Thank you @kplatis and @sboutas for the suggestion.

I tried that module, but it doesn’t seem to work as I want for the two following reasons:

  1. I want to be able to select from the respective menu link if the page will be opened in a new tab or not, but this module doesn’t have this feature. Fortunately, I solved my problem when I enabled Menu Attributes module
  2. It doesn’t recognize the other Cern pages as external links, because they are under the same domain (cern.ch)

Apart from menu links I would like to have the similar functionality (to open external links in a new tab) for Article box, could you add this functionality as a check box under the selected link (inside the LINK of the following image)?

article%20box%20external%20link

Best regards,
Ioannis

Hi Ioanni,

Actually the module works fine. I have just enabled it in one of my test sites. Then I enable the option to open external links in new windows and worked ad expected. You can also specify patterns on which type of links should be excluded.

external_links

Thank you very much @sboutas,

I unchecked “Exclude links with the same primary domain” and it worked perfectly.

In my case your solution was what I wanted. I guess my article only make sense when you want to open an internal link in a new page or you want it to do it exceptionally for few external links. I will update it!

Ioannis

Hello everyone,

In my case @sboutas proposal to use External Links solved my problem with external links, because I wanted to apply this functionality to each and every external links.

Nevertheless, someone may want to do the same functionality for internal links or few exceptional external links. I would like to write the following article in order to share the solutions I found for the respective problem with you.

Opening external links in a new tab
Just install External Links module and uncheck “Exclude links with the same primary domain” from module’s settings


Opening an internal link in a new tab (or an exceptional external link)

You may want to open an external link (or even an internal one) in a new tab. Two of the most common cases are the following:

  1. Links in pages, a custom blocks or headers / footers of view:
    Once you have written your html code using the “UnfilteredText format, just add target="_blank" inside the <a> tag of the link you want it to be opened in a new tab

  2. Links in the main menu:
    At first you will need to enable Menu Attributes module. Then, go to the menu link you want it to be opened in a new tab, open Menu link attributes and select “_New windows (blank)” as a Target

I hope it will be helpful,
Ioannis

2 Likes