Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use javascript to intercept all document link clicks

Tags:

How do I intercept link clicks in document? It must be cross-platform.

I am looking for something like this:

// content is a div with innerHTML
var content = document.getElementById("ControlPanelContent");

content.addEventListener("click", ContentClick, false);

 function ContentClick(event) {
 
    if(event.href == "http://oldurl")
  {
     event.href = "http://newurl";
  }
} 
like image 717
Jeremy Gwa Avatar asked Jan 25 '10 23:01

Jeremy Gwa


People also ask

How do you find a click on a link?

Tracking link clicks on websites For websites, you can use Google Analytics. To do this, enable the analytics tools provided by Google and use their measurements to check all your clicked links arriving at the website. If you use marketing channels to mostly drive traffic to your website, this is a good place to start.


3 Answers

What about the case where the links are being generated while the page is being used? This occurs frequently with today's more complex front end frameworks.

The proper solution would probably be to put the click event listener on the document. This is because events on elements propagate to their parents and because a link is actually acted upon by the top-most parent.

This will work for all links, whether they are loaded with the page, or generated dynamically on the front end at any point in time.

function interceptClickEvent(e) {
    var href;
    var target = e.target || e.srcElement;
    if (target.tagName === 'A') {
        href = target.getAttribute('href');

        //put your logic here...
        if (true) {

           //tell the browser not to respond to the link click
           e.preventDefault();
        }
    }
}


//listen for link click events at the document level
if (document.addEventListener) {
    document.addEventListener('click', interceptClickEvent);
} else if (document.attachEvent) {
    document.attachEvent('onclick', interceptClickEvent);
}
like image 198
James Avatar answered Nov 04 '22 08:11

James


for (var ls = document.links, numLinks = ls.length, i=0; i<numLinks; i++){
    ls[i].href= "...torture puppies here...";
}

alternatively if you just want to intercept, not change, add an onclick handler. This will get called before navigating to the url:

var handler = function(){
    ...torment kittens here...
}
for (var ls = document.links, numLinks = ls.length, i=0; i<numLinks; i++){
    ls[i].onclick= handler;
}

Note that document.links also contains AREA elements with a href attribute - not just A elements.

like image 22
Roland Bouman Avatar answered Nov 04 '22 06:11

Roland Bouman


I just found this out and it may help some people. In addition to interception, if you want to disallow the link to load another page or reload the current page. Just set the href to '#' (as in internal page ref prefix). Now you can use the link to call a function while staying at the same page.

like image 20
worldsayshi Avatar answered Nov 04 '22 07:11

worldsayshi