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: http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CDcQFjAB&url Code (markup): My script: $(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; } } }); }); Code (markup): This is exactly what I've been trying to do. However, with this script, the problem is - http://localhost/wordpress/redirect.php?link= Code (markup): part gets prefixed on only one link. Suppose I have a lot of different website links like bing.com, digitalpoint.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 digitalpoint.com link... nothing gets prefixed. What am I missing? Hope I've been descriptive enough
if (count == 0) { $(this).attr('href', "http://localhost/wordpress/redirect.php?link=" + encodeURIComponent($(this).attr('href'))); $(this).attr('target', '_blank'); count = count + 1; } HTML: Here is your issue, the moment you find your first match, count is not anymore 0, so it only works on the first link Not sure why yu have the count anyway, from what i see, its of no use at all
I would use each as opposed to onmousedown, unless you want to hide the url then use onmousedown with js redirect: $(document).ready(function () { var linkArray = [ "google.com", "yahoo.com" ]; $('a').each(function() { for (var i = 0; i < linkArray.length; i++) { if ($(this).attr('href').indexOf(linkArray[i]) > 0) { return; } } $(this).attr('href', "http://localhost/wordpress/redirect.php?link=" + encodeURIComponent($(this).attr('href'))); $(this).attr('target', '_blank'); }); }); Code (markup):