Open External Menu Links in New Window

Use this JS function when you can't edit your menu markup in order to put target="_blank" on external links. You can optionally open sub-domains in a new window too.

Works in all browsers and IE8+

View the source of this page to see the JS.

Demo 1

Inspect the menu below in the developer console and you'll see target="_blank" applied to the external 'Fast Edit' link.

Demo 2

However, if you inspect this second demo you'll see that target="_blank" has been applied to the external 'Fast Edit' link and the 'Blog' sub-domain link.

Usage

The navExtTargetBlank() JavaScript function takes 2 parameters;

  1. The CSS selector of the menu (the function will find all <a> elements within the menu)
  2. A keyword for where to open sub-domains ('subdomain-same-window' or 'subdomain-blank-window')

Pop it above the closing </body> tag.

More demos and snippets

Did you find this useful? There are more demos and code snippets this way.