Rewrite and log links with jQuery
I log and rewrite my outgoing links with jquery. I do this for a couple of reason:
I want to have valid XHTML, and not add target to my links in the markup.
I want Google to index my outbound links correctly and not have mysterious links like /link/out.php?url=http://www.yahoo.com.
I want the sites that i link to, to see the correct referrer in their logs.
I still want to log what links are popular exits from my page
How do I solve this?
Add a class to each external link I want to log.
[Yahoo!](http://www.yahoo.com)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>
- Create the server-side controller for handling logging.
<code>
out.php
</code>
That should more or less do it.
We still have valid strict xhtml
The outbound links are unaltered in a Search Engine Optimization point of view
Referring links are correct for those sites that we do link to
We are able to log outbound links