hising.net

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!](http://www.yahoo.com)

  2. Attach a log and a rewrite behaviour to links with the class added above.

<code>
$(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){});
     }
  });
}
);
</code>
  1. Create the server-side controller for handling logging.
<code>
out.php

</code>

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

Categories

Tags

  • jQuery