Is it possible to run a code or a function on link click?

in Script development
Subscribe to Is it possible to run a code or a function on link click? 4 posts, 3 voices



tckman Scriptwright
ChromeWindows

Is it possible to run a code or a function on link click to manipulate the clicked link url?
Thanks

 
Monkey2000 User
FirefoxWindows

You need to attach an event handler to the link (element.addEventListener). Then you can do whatever you want.

See Pitfall #2 here: http://www.oreillynet.com/pub/a/network/2005/11...

 
tckman Scriptwright
ChromeWindows

Thanks for the reply.
Can I set this event handler to all links without parsing and identifying each one of them?
Doing so will make my script process links that the user would never click.

 
Jefferson Scher Scriptwright
FirefoxWindows

Some events can be intercepted by an element that contains the element that's actually clicked. You could try adding a click event listener on the page and see whether you can detect it there.

function gotClicked(e){
  if (e.target.nodeName.toLowerCase() = "a"){ // it's a link!
    // some actually useful code
  }
}