Rewrite and log links with jQuery

I log and rewrite my outgoing links with jquery. I do this for a couple of reason:

  1. I want to have valid XHTML, and not add target to my links in the markup.
  2. I want Google to index my outbound links correctly and not have mysterious links like /link/out.php?url=http://www.yahoo.com.
  3. I want the sites that i link to, to see the correct referrer in their logs.
  4. I still want to log what links are popular exits from my page

How do I solve this?

  1. Add a class to each external link I want to log.

    Yahoo!
  2. Attach a log and a rewrite behaviour to links with the class added above.
    
    $(document).ready(function(){
      $("a.external-link").each(function(){
         //rewrite: open in new window
         this.target = "_blank";
         //log the click
         this.onclick = function(){
           //lets make some valid use of xmlhttp-capabilities
           $.get("/out.php?url=" + escape(this.href), function(s){});
         }
      });
    }
    );
    
    
  3. Create the server-side controller for handling logging.
    
    out.php
    
    
    

That should more or less do it.

  1. We still have valid strict xhtml
  2. The outbound links are unaltered in a Search Engine Optimization point of view
  3. Referring links are correct for those sites that we do link to
  4. We are able to log outbound links

Related posts

  1. jQuery plugin for logging links
  2. Miscellaneous jQuery links
  3. Table sorter plugin for jquery
  4. jQuery Plugin for using default value as label
  5. Yahoo Graded Browser Support

One thought on “Rewrite and log links with jQuery

  1. Pingback: 301 Moved Permanently

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>