PHP
  Home arrow PHP arrow Page 3 - Standards-compliant Link Targets with ...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    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

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway