0

I have created various menus, via the menu structure->menu page in Drupal for various pages in my site. I want to have some, if not all, of these menu links to open in a new page. You can not add target="_blank" to the path in the menu screen and I don't want to add a new module right now.

I am trying this:

Drupal.behaviors.externalMenu = function (context) {
  if (jQuery().attr) {
$(document).ready(function(){
  $('.page-node-166 a', context).attr('target', '_blank');
});
}}

My Steps: created a new js file, added the above code, went into my themes .info file, referenced this js file in the scripts section, refreshed, a few times, nothing works. Links still open in current window.

What should be happening in this code snippet is, find this class[page-node-166], which is the page, then for any link on that page, open it in new window. My first time attempting this so I don't know if I am missing a step.

How can I have menu links open in new window in drupal 7?

=====================

Update: enter image description here

  • Remove context. See my update below – chadpeppers Oct 3 '14 at 15:08
0

You don't really need a drupal behavior to do this. Instead just simple do this:

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

or

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

By default drupal uses jQuery.noConflict() so that it does not interfere with the $ that may be used from other libraries.

| improve this answer | |
  • Hi Chad - thanks for the quick reply. I tried both solutions, but get the same error. note image above. I have double check, the class is spelled and referenced correctly. any ideas? – user1176783 Oct 3 '14 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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