PHP
  Home arrow PHP arrow Page 3 - Standards-compliant Link Targets with Wordpress
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Standards-compliant Link Targets with Wordpress
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-12-19


    Table of Contents:
  • Standards-compliant Link Targets with Wordpress
  • Dynamically adding rel=’no follow’
  • JavaScript link targets
  • Putting it all together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Standards-compliant Link Targets with Wordpress - JavaScript link targets
    ( Page 3 of 4 )

    Recent XHTML standards have depreciated the use of the target attribute for controlling link targets.  Pages containing this attribute will no longer validate.

    The reasoning is that modern browsers give the user the ability to open links wherever they want and that web developers should leave the decision up to the reader.  While I don’t completely disagree with that philosophy, I choose not to send my visitors away when sending them to other sites.

    As a courtesy, you might consider notifying readers whenever a link opens a new window.

    To get around the limitations imposed by the new standards, you can use a JavaScript to target your links.  This prevents hard coding the target attribute into your page.  The validator is happy and your links still target wherever you want them to.

    The JavaScript that performs this task is actually pretty simple.  To begin, you need to create a function and gather all of the links on your page.

    /*<![CDATA[*/

    function outboundLinks() {

       if (!document.getElementsByTagName) return;

       var anchors = document.getElementsByTagName("a");

    The first line of the function checks to make sure that the getElementsByTagName function is supported by the current browser.  If it isn’t, the function simply exits before any errors are generated.  The second line searches the document tree for any link elements (<a>) and returns those objects as an array.

       for (var i=0; i<anchors.length; i++) {

           var anchor = anchors[i];

           if (anchor.getAttribute("href") &&

              anchor.getAttribute("rel") == "nofollow") {

                  anchor.target = "_blank";

           }

    Next, a "for" loop iterates through each of the array elements and checks for any links that contain both a valid destination and a rel=”nofollow” attribute.  It also adds the target attribute that we need.  Because of our Wordpress plugin, this code will only change external links (since they are the only ones with the rel=”nofollow” attribute.

       }

    }

    window.onload = function() {

       if (document.body) outboundLinks();

    };

    /*]]>*/

    Finally we close our function and add it to the document’s onload event, so that it is executed whenever the page is fully loaded.

    Save this file as external.js and upload it to your web site.



     
     
    >>> More PHP Articles          >>> More By Nilpo
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT