To-Go Menu

Have you ever wanted to have a menu item open in a new window, when clicked? The standard menu dialog does not offer the option of a target for the menu link. So, what to do? A little jQuery magic!

1. Inspect the menu item, or view the page source, and find it's id...something like this:

<div id="menu-231">
  <a href="wherever">My menu link</a>
</div>

 

2. Add the following snippet to your theme. How you will add it depends...see the footnote at the end:

$(document).ready(function(){
  $('#menu-231 a').each(function(){
    $(this).attr({ target: "_blank" });
  });
});

This will add a target to the link.

Footnote

There are a number of options to get jQuery injected onto the page.

1. If your theme has a global js file, you can add the snippet to that file.

2. Create a .js file that holds the snippet and list the file as a script file in the theme .info file.

3. Add the snippet to the theme's page.tpl.php file. Be sure to wrap it between <script type="text/javascript"> and </script>.

3. In template.php, you can add a drupal_add_js() call to theme_preprocess_page().

4. You can call drupal_add_js() from within a custom module.


Type:
Tutorial
Tags:
jQuery