5

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?

1
  • 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, 2011 at 13:34

6 Answers 6

12

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.

4
  • I'm using superfish menu on zen theme and my menus doesn't have own ID thanks,
    – jone
    Jun 6, 2011 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, 2011 at 13:58
  • I didn't know about the Menu attributes module, thanks. Jun 6, 2011 at 16:02
  • Thanks menu attribute works perfect Thanks again.
    – jone
    Jun 7, 2011 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.

1
  • 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, 2011 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.

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