WordPress.org

Ready to get started?Download WordPress

Forums

target="_blank" (6 posts)

  1. cableghost
    Member
    Posted 5 years ago #

    As reported by W3C some time ago, target="_blank" is not good authoring and will have an impact.

    I wonder if you could, instead, implement a jscript function to open external links in a new window.

    -Scott

  2. stvwlf
    Member
    Posted 5 years ago #

    Before:
    external link
    After:
    external link

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

    As this is the kind of script you'll want to deploy across your entire site, you should copy this code into a separate file (e.g. external.js), and then load it in every page on your site with the following code, which should appear in the <head> tag of each document:
    <script type="text/javascript" src="/external.js">
    </script>

  3. stvwlf
    Member
    Posted 5 years ago #

    oops

    where it said

    external link

    external link

    its supposed to say

    Before:
    <a href="document.html" target="_blank">external link</a>
    After:
    <a href="document.html" rel="external">external link</a>
  4. cableghost
    Member
    Posted 5 years ago #

    Thank you!

    When you say add the script inside the Head section of each document, does that mean every php file that has a head section? For my theme, I'm wondering if I could just add it to my header.php

    -Scott

  5. stvwlf
    Member
    Posted 5 years ago #

    that's what it means translated into WP - header.php - these are quotes from an old article

  6. cableghost
    Member
    Posted 5 years ago #

    Thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic