?
Solved

Add event listener to all links

Posted on 2012-08-17
5
Medium Priority
?
502 Views
Last Modified: 2012-09-03
How can I add an onclick event listener to all links on a page, not using jquery etc.
0
Comment
Question by:Gary
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:SamuelShaw
ID: 38305067
You can use document.GetElementsByTagName to get all the "a" elements and loop through them. If you want to be more thorough, you'll check that they have a value for the "href" attribute before binding your event.
var linkList = document.getElementsByTagName("a");
for (var nIndex = 0; nIndex < linkList.length; ++nIndex) {
	var sTarget = linkList[nIndex].getAttribute("href");
	if (sTarget != "") {
		linkList[nIndex].onclick = function () {
		// Do what you want to do when clicking a link
		};
	}
}

Open in new window

0
 
LVL 58

Author Comment

by:Gary
ID: 38305215
That works but it overwrites any existing onclick events
0
 
LVL 2

Expert Comment

by:SamuelShaw
ID: 38305744
Right. Define the onClick function separately and try the following:
if (linkList[nIndex].addEventListener) {
	linkList[nIndex].addEventListener("click", MyOnClickHandler, false);
} else if (linkList[nIndex].attachEvent) {
	linkList[nIndex].attachEvent("onclick", MyOnClickHandler, false);
} else {
	if ((linkList[nIndex].onclick != "") && (/;\s*$/.test(!linkList[nIndex].onclick)))
		linkList[nIndex].onclick += ";";
	linkList[nIndex].onclick += "MyOnClickHandler()";
}

Open in new window

0
 
LVL 58

Author Comment

by:Gary
ID: 38306287
I don't want to write an onclick event to every link, I just want to capture it when it is clicked.

Half solved it with this but its capturing all click events.
if (window.captureEvents){
window.captureEvents(Event.CLICK);
window.onclick=sourceCheck;
}
else
document.onclick=sourceCheck;

function sourceCheck(e){
var el=(typeof event!=='undefined')? event.srcElement : e.target
if(el.tagName=="A"){
// Do something
}
}
0
 
LVL 2

Accepted Solution

by:
SamuelShaw earned 2000 total points
ID: 38312026
That's right. You can put a document-wide onClick event and look for the original target element. It will capture all of the click events. You cannot capture just the A clicks without putting onClick handlers on all the A elements.
<script type="text/javascript">
function onClick(objEvent) {
	objEvent = objEvent || window.event;
	// IE and Chrome support objEvent.srcElement
	if (objEvent.srcElement)
		alert("IE/Chrome target = " + objEvent.srcElement.tagName);
	// FF and Chrome support objEvent.target
	else if (objEvent.target)
		alert("FF target = " + objEvent.target.tagName);
}
document.onclick = onClick;
</script>

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question