wp_nav_menu – Make Links Open In A New Window

Taken from here: http://techstudio.co/web-design/the-best-way-to-make-links-open-in-a-new-window

The old method for setting links up to open in a new window causes validation errors in xHTML.

<code><a href=</code><code>"#"</code> <code>target=</code><code>"_blank"</code><code>>link</a></code>

The right way is to use the following markup.

<code><a href=</code><code>"#"</code> <code>rel=</code><code>"external"</code><code>>link</a></code>

But for that markup to work the following Javascript must be included in your document.

<code>function</code> <code>externalLinks() {</code>
<div><code>     </code><code>if</code> <code>(!document.getElementsByTagName) </code><code>return</code><code>;</code></div>
<div><code>     </code><code>var</code> <code>anchors = document.getElementsByTagName(</code><code>"a"</code><code>);</code></div>
<div><code>     </code><code>for</code> <code>(</code><code>var</code> <code>i=0; i<anchors.length; i++) {</code></div>
<div><code>          </code><code>var</code> <code>anchor = anchors[i];</code></div>
<div><code>          </code><code>if</code> <code>(anchor.getAttribute(</code><code>"href"</code><code>) && anchor.getAttribute(</code><code>"rel"</code><code>) == </code><code>"external"</code><code>) anchor.target = </code><code>"_blank"</code><code>;</code></div>
<div><code>     </code><code>}</code></div>
<div><code>}</code></div>

Revisions

Tags:

No comments yet.

Leave a Reply