We help IT Professionals succeed at work.

How do I capture a link click event

pcorreya asked
I need to capture the href value when a user click on any links on the page. Can I do it as a global event capture, everytime someone clicks a link.
Watch Question

this is a simple example of how to get the link value. But if u want to capture it into the database or whatsoever you need server side scripts.

<a href="abc.htm" onClick="javascript: alert(this.href);">dsd</a>

You can do it for all href's too. Use the following script:

function clickhref(obj)

function doc_onload()
    document.links[i].onclick=new function(){clickhref(this);}

If some of the links are already using the onclick handler, then the above won't work. Try this approach:

1) add left click handler to the document
2) in the handler, if the event source is an <a> tag, get its href property, then throw the event again or exam the href value, ie if "javascript:" do an "eval()" or just do "document.location.href = href".




I called the doc_onload() from the onload event and it keeps executing the function clickhref for each href and displays an alert box with object undefined.

The solution you have given me is what I want if I can get it to work.




There is already an onclick event on one link, so I take it that is why CJ's code it not working. Can you elaborate on your possible workaround.

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
POINTS FOR EXPERTS awaiting comments are listed in the link below
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
Thanks everyone.
Moderator @ Experts Exchange
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **

Explore More ContentExplore courses, solutions, and other research materials related to this topic.