onMouseDown event help

by roady
5 replies
Hi guys,


Im having some problem with onMouseDown event.


So like when you go to Google and click on a search result... something like this gets prefixed to the landing page:


Code:
http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CDcQFjAB&url
My script:


Code:
$(document).ready(function () {
  var count = 0;
  var matchavailable = 0;
  var protectedlinks = "google.com,yahoo.com";
  $("a").mousedown(function () {
  var linkArray = protectedlinks.split(',');
  for (var i = 0; i < linkArray.length; i++) {
  if ($(this).attr('href').indexOf(linkArray[i]) > 0) {
  matchavailable = 1;
  break;
  }
  else {
  matchavailable = 0;
  }
  }


  if (matchavailable == 0) {
  if (count == 0) {
  $(this).attr('href', "http://localhost/wordpress/redirect.php?link=" + encodeURIComponent($(this).attr('href')));
  $(this).attr('target', '_blank');
  count = count + 1;
  }
  }


  });




});
This is exactly what I've been trying to do. However, with this script, the problem is -

Code:
http://localhost/wordpress/redirect.php?link=
part gets prefixed on only one link.


Suppose I have a lot of different website links like bing.com, warriorforum.com...the thing is, if I click on bing.com I get that redirect.php prefixed. However, right after that, when I try to do it on warriorforum .com link... nothing gets prefixed (and vica versa). What am I missing?


Hope I've been descriptive enough
#event #onmousedown
  • Profile picture of the author Nathan K
    Its only working on one link because of the count variable. Initially it is set to 0 but after you click a link the value of the count variable is incremented by 1. If you click a link again the condition count == 0 is not met. To solve this remove the condition or change it.

    Code:
    $(document).ready(function () {
      var count = 0;
      var matchavailable = 0;
      var protectedlinks = "google.com,yahoo.com";
      $("a").mousedown(function () {
      var linkArray = protectedlinks.split(',');
      for (var i = 0; i < linkArray.length; i++) {
      if ($(this).attr('href').indexOf(linkArray[i]) > 0) {
      matchavailable = 1;
      break;
      }
      else {
      matchavailable = 0;
      }
      }
    
      if (matchavailable == 0) {
        $(this).attr('href', "http://localhost/wordpress/redirect.php?link=" + encodeURIComponent($(this).attr('href')));
        $(this).attr('target', '_blank');
        count = count + 1;
      }
      });
    });
    {{ DiscussionBoard.errors[8503715].message }}
  • Profile picture of the author brutecky
    Looks like your writing a script to try to jack peoples link clicks and send them to affiliate offers of yours.
    {{ DiscussionBoard.errors[8505148].message }}
  • Profile picture of the author roady
    Yeah and its pretty much listed in the Privacy Policy/Affiliate Disclosure on my website
    Signature

    {{ DiscussionBoard.errors[8507085].message }}
  • Profile picture of the author BuddhaOfCode
    Evil.. Who reads TOS !!
    {{ DiscussionBoard.errors[8507299].message }}
  • Profile picture of the author roady
    Actually, it's not just affiliate links. Moreover, my main priority is to track the clicks using a semi-proprietary software. Anyway, thanks for the help!
    Signature

    {{ DiscussionBoard.errors[8511143].message }}

Trending Topics