Posted By

flyingcoyote on 02/25/10


Tagged

javascript link jquery external tracking outbound


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

johnloy
Priestd09


Automatically redirect all outbound links on site through disclaimer/tracker page WITHOUT editing them individually


 / Published in: jQuery
 

This script will automatically route all outbound links through a PHP (or whatever scripting language you prefer) page that can be used for a "You are leaving..." disclaimer and/or click tracking.

Devised for a site that uses a store app, a forum app, and a CMS app that don't talk to each other that well; it would have taken forever to hack all those apps to do the same thing server-side.

DOMAIN-NAME-HERE = YOUR DOMAIN NAME

TLD-HERE = YOUR TLD, IE, "COM" OR "NET"

LINKOUTPAGE.PHP = YOUR TRACKING PAGE (CALLED WITH TARGET URL APPENDED AS "p=[outside url]")

  1. $(document).ready(function() {
  2.  
  3. // BEGIN redirect all outbound links through tracker page
  4. $("a").click(function(){
  5.  
  6. // if it's a full URL...
  7. if ($(this).attr("href").indexOf("http")==0) {
  8.  
  9. // if it doesn't go to our site
  10. if (!/^http(s){0,1}(.){0,3}(www){0,3}(\.){0,1}DOMAIN-NAME-HERE\.TLD-HERE/.test($(this).attr("href"))){
  11.  
  12. // send it through the linkout page
  13. $(this).attr("href","/LINKOUTPAGE.PHP?p=" + $(this).attr("href"))
  14.  
  15. }
  16. }
  17. })
  18. // END redirect all outbound links through tracker page
  19.  
  20. };

Report this snippet  

You need to login to post a comment.