Ulysses69
11-14-2012, 07:07 PM
I have been working on a script to find links and pass on analytical data when clicked. The first part I can do, but I'm struggling to find a way to tidy way to get desired parent ID. The idea is to find parent of clicked link, if it is undesired (such as child UL or empty) then find next parent up until we find an ID. All my links will be within ID parent, except for those immediately children of body (which will use body ID). These links are generated in a custom CMS so body tag always have an ID and so do UL and DIVs that will contain links ... some client-generated links could be created without ID hence why I am having the body as a catchall.
Anyways, if we have something like DIV > UL > LI > UL > LI > A and I click A, I want href or title to be passed along with top-most UL or DIV ID to be passed too.
The general parent ID works this way:
if (targ.parentNode.id != '') {
dad = targ.parentNode;
} else if (targ.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode;
} else if (targ.parentNode.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode.parentNode;
} else if (targ.parentNode.parentNode.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode.parentNode.parentNode;
}
But I am trying to mimize the code so that I can start to add some exclusions and/or other intelligence to handle what ID should be used, and it isn't quite working:
while(targ.parentNode && targ.parentNode.id != '' && tagx.indexOf(targ.parentNode.tagNode) == -1) {
targ = targ.parentNode;
}
dad = targ;
The full page code to test might help you see what I am doing:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Find Parent Id</title>
<script type="text/javascript">
function assignClickEvent(){
var links = document.links;
for(var i =0; i < links.length; i++){
links[i].onclick = findDivId;
}
};
function findDivId(e) {
var targ;
var dad;
var idx = new Array('nav');
var tagx = new Array('BODY','P','LI','A');
if (!e){
var e = window.event;
}
if (e.target){
targ = e.target;
} else if (e.srcElement) {
targ = e.srcElement;
}
while(targ.parentNode && targ.parentNode.id != '' && tagx.indexOf(targ.parentNode.tagNode) == -1) {
targ = targ.parentNode;
}
dad = targ;
/*
if (targ.parentNode.id != '') {
dad = targ.parentNode;
} else if (targ.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode;
} else if (targ.parentNode.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode.parentNode;
} else if (targ.parentNode.parentNode.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode.parentNode.parentNode;
}
*/
if(dad){
//if(idx.indexOf(dad.id) == -1){
alert(dad.id + ' ' + dad.tagName + ' ' + dad.href);
//}
};
return false;
};
window.onload = assignClickEvent;
</script>
</head>
<body id="page">
<div id="main">
<h2>Header</h2>
<div>
<ul>
<li><a href="scot.html">Scotiabank</a> (6)</li>
<li><a href="mon.html">Bank of Montreal</a> (3)</li>
<li><a href="cibc.html">CIBC</a> (3)</li>
<li><a href="canada.html">National Bank of Canada</a> (2)</li>
</ul>
</div>
<h2>Main</h2>
<ul id="nav">
<li><a href="scot.htm">Scotiabank</a> (6)
<ul id="nav-sub">
<li id="cib-sub"><a href="scot.html">Scotiabank</a> (6)</li>
<li><a href="mon.html">Bank of Montreal</a> (3)</li>
<li><a href="canada.html">National Bank of Canada</a> (2)</li>
</ul></li>
<li><a href="mon.html">Bank of Montreal</a> (3)</li>
<li><a href="cibc.html">CIBC</a> (3)</li>
<li><a href="canada.html">National Bank of Canada</a> (2)</li>
</ul>
</div>
<h2>Footer</h2>
<ul>
<li><a href="scot.html">Scotiabank</a> (6)</li>
<li><a href="mon.html">Bank of Montreal</a> (3)</li>
<li><a href="cibc.html">CIBC</a> (3)</li>
<li><a href="canada.html">National Bank of Canada</a> (2)</li>
</ul>
</body>
</html>
Anyways, if we have something like DIV > UL > LI > UL > LI > A and I click A, I want href or title to be passed along with top-most UL or DIV ID to be passed too.
The general parent ID works this way:
if (targ.parentNode.id != '') {
dad = targ.parentNode;
} else if (targ.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode;
} else if (targ.parentNode.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode.parentNode;
} else if (targ.parentNode.parentNode.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode.parentNode.parentNode;
}
But I am trying to mimize the code so that I can start to add some exclusions and/or other intelligence to handle what ID should be used, and it isn't quite working:
while(targ.parentNode && targ.parentNode.id != '' && tagx.indexOf(targ.parentNode.tagNode) == -1) {
targ = targ.parentNode;
}
dad = targ;
The full page code to test might help you see what I am doing:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Find Parent Id</title>
<script type="text/javascript">
function assignClickEvent(){
var links = document.links;
for(var i =0; i < links.length; i++){
links[i].onclick = findDivId;
}
};
function findDivId(e) {
var targ;
var dad;
var idx = new Array('nav');
var tagx = new Array('BODY','P','LI','A');
if (!e){
var e = window.event;
}
if (e.target){
targ = e.target;
} else if (e.srcElement) {
targ = e.srcElement;
}
while(targ.parentNode && targ.parentNode.id != '' && tagx.indexOf(targ.parentNode.tagNode) == -1) {
targ = targ.parentNode;
}
dad = targ;
/*
if (targ.parentNode.id != '') {
dad = targ.parentNode;
} else if (targ.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode;
} else if (targ.parentNode.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode.parentNode;
} else if (targ.parentNode.parentNode.parentNode.parentNode.id != '') {
dad = targ.parentNode.parentNode.parentNode.parentNode;
}
*/
if(dad){
//if(idx.indexOf(dad.id) == -1){
alert(dad.id + ' ' + dad.tagName + ' ' + dad.href);
//}
};
return false;
};
window.onload = assignClickEvent;
</script>
</head>
<body id="page">
<div id="main">
<h2>Header</h2>
<div>
<ul>
<li><a href="scot.html">Scotiabank</a> (6)</li>
<li><a href="mon.html">Bank of Montreal</a> (3)</li>
<li><a href="cibc.html">CIBC</a> (3)</li>
<li><a href="canada.html">National Bank of Canada</a> (2)</li>
</ul>
</div>
<h2>Main</h2>
<ul id="nav">
<li><a href="scot.htm">Scotiabank</a> (6)
<ul id="nav-sub">
<li id="cib-sub"><a href="scot.html">Scotiabank</a> (6)</li>
<li><a href="mon.html">Bank of Montreal</a> (3)</li>
<li><a href="canada.html">National Bank of Canada</a> (2)</li>
</ul></li>
<li><a href="mon.html">Bank of Montreal</a> (3)</li>
<li><a href="cibc.html">CIBC</a> (3)</li>
<li><a href="canada.html">National Bank of Canada</a> (2)</li>
</ul>
</div>
<h2>Footer</h2>
<ul>
<li><a href="scot.html">Scotiabank</a> (6)</li>
<li><a href="mon.html">Bank of Montreal</a> (3)</li>
<li><a href="cibc.html">CIBC</a> (3)</li>
<li><a href="canada.html">National Bank of Canada</a> (2)</li>
</ul>
</body>
</html>