6

When I try putting a link like <a href="http://www.example.com/" target="_blank"> on the link path, I get the following error message:

The path '//www.example.com/" target="_blank">' is either invalid or you do not have access to it.

Is there a way to have the external URL opened in a new tab?

  • If you are developing a module you could use url_is_external to see if a URL is external. Then you could add a target based on the return value. – Bart Jun 24 '11 at 13:34
13

If you can target your menu tag with jquery using its ID you can put something like this in an JS file that you'll add in your theme via your_theme.info

Drupal.behaviors.externalMenu = function(context, settings) {
    $('#menu_id').attr('target', '_blank');
};

Or you can use the Menu attributes module.

  • I'm using superfish menu on zen theme and my menus doesn't have own ID thanks, – jone Jun 6 '11 at 12:06
  • 1
    Yes that's a problem in some situations, hopefully you can use a module like Menu attributes to add some custom attributes and it supports the "target" attributes so I think you get your answer, I'll edit mine. – tostinni Jun 6 '11 at 13:58
  • I didn't know about the Menu attributes module, thanks. – Jamie Hollern Jun 6 '11 at 16:02
  • Thanks menu attribute works perfect Thanks again. – jone Jun 7 '11 at 5:48
3

Not per menu item. However, the External links project allows to open all external links in a new window and indicates them as such with an icon.

  • thank you! i tried that module, i don't want to open all external link by new window. i have one menu and i want to open that menu link on a new tab. – jone Jun 6 '11 at 11:02
1

The problem is that you are entering <a href="http://www.example.com/" target="_blank">, when Drupal is expecting only the link (http://www.example.com, in your case).

If you would enter http://www.example.com, Drupal would notice it's an external link, and it will allow you to use it.
I tried adding a link to drupal.org, and as you can see, Drupal accepted it (it's the link shown as "Drupal").

screenshot

For how to open the link in a new browser tab, the other answers already explain how to do that.

0

There's a project called "Menu Target" that allows you to do just that. Maybe that helps those who only need individual menu links to open in a new browser.

0

If you need to make all external links open in a new window you can rewrite theme_links in your current theme, if not use menu_attributes module as given above.

function yourtheme_name_link($variables) {
  // Add target blank to all external links.
  if (url_is_external($variables['path'])) {
    $variables['options']['attributes']['target'] = '_blank';
  }
  return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</a>';
}
0

I just got this answer from the board which was a big help, this may work for you as well:

(function($){
  $(document).ready(function(){
    $('.page-node-166 a').attr('target', '_blank');
  });
})(jQuery);

in my case, i referenced the page class, then the 'a' and that made the links on that page open in new window. I am going to do a bit more selector chaining to I can better pinpoint the specific menu in the sidebar.

protected by Community Oct 6 '18 at 12:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.