Hi,
I am learning JavaScript and I am using Christian Heilmann’s book to do so.
I am struggling on a piece of code.
[code]
closestSibling:function(node,direction){
var tempObj;
if(direction==-1 && node.previousSibling!=null){
tempObj=node.previousSibling;
while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
tempObj=tempObj.previousSibling;
}
}else if(direction==1 && node.nextSibling!=null){
tempObj=node.nextSibling;
while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){
tempObj=tempObj.nextSibling;
}
}
return tempObj.nodeType==1?tempObj:false;
}
I’m actually confused by this:
[code]
while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
tempObj=tempObj.previousSibling;
}
and
[code]
return tempObj.nodeType==1?tempObj:false;
Any help or advice would be most appreciated.
<i>
</i>while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
tempObj=tempObj.previousSibling;
}
<i>
</i>while(tempObj is a DOM element and therefore has a previousSibling AND tempObj is NOT the first born)
{
resign tempObj to reference it's older brother/sister;
}
<i>
</i>return tempObj.nodeType==1?tempObj:false;
<i>
</i>if(tempObj is an element node)
return tempObj;
else
return false;
<i>
</i>closestSibling:function(node,direction)
<i>
</i>while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
tempObj=tempObj.previousSibling;
}
[CODE]function findSib(node,direction){
var X= (direction!=-1)? 'nextSibling': 'previousSibling';
if(node[X]== null) return false;
var sib= node[X];
while(sib){
if(sib.nodeType== 1) return sib;
sib= sib[X];
}
return false;
}[/CODE]
<i>
</i>while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
tempObj=tempObj.previousSibling;
}
while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
tempObj=tempObj.previousSibling;
}[/QUOTE]
<i>
</i>sc = {
init:function(){
sc.head = document.getElementsByTagName('h3')[0];
if(!sc.head){return;}
sc.ad = DOMhelp.closestSibling(sc.head,1);
sc.ad.style.display='none';
var t = DOMhelp.getText(sc.head);
var collapseLink = DOMhelp.createLink('#',t);
sc.head.replaceChild(collapseLink,sc.head.firstChild);
DOMhelp.addEvent(collapseLink,'click',sc.peekaboo,false)
collapseLink.onclick = function(){return;} // Safari Fix
},
peekaboo:function(e){
sc.ad.style.display=sc.ad.style.display=='none'? '':'none';
DOMhelp.cancelLink(e);
}
}
DOMhelp.addEvent(window,'load',sc.init,false);
<i>
</i><body>
<h3>Contact Details</h3>
<address>
Awesome Web Production Company<br />
Going Nowhere Lane 0<br />
Catch 22<br />
N4 2XX<br />
England<br />
</address>
</body>
<i>
</i>DOMhelp=
{
// Find the last sibling of the current node
lastSibling:function(node){
var tempObj=node.parentNode.lastChild;
while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
tempObj=tempObj.previousSibling;
}
return (tempObj.nodeType==1)?tempObj:false;
},
<i> </i>// Find the first sibling of the current node
<i> </i>firstSibling:function(node){
<i> </i> var tempObj=node.parentNode.firstChild;
<i> </i> while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){
<i> </i> tempObj=tempObj.nextSibling;
<i> </i> }
<i> </i> return (tempObj.nodeType==1)?tempObj:false;
<i> </i>},
<i> </i>// Retrieve the content of the first text node sibling of the current node
<i> </i>getText:function(node){
<i> </i> if(!node.hasChildNodes()){return false;}
<i> </i> var reg=/^s+$/;
<i> </i> var tempObj=node.firstChild;
<i> </i> while(tempObj.nodeType!=3 && tempObj.nextSibling!=null || reg.test(tempObj.nodeValue)){
<i> </i> tempObj=tempObj.nextSibling;
<i> </i> }
<i> </i> return tempObj.nodeType==3?tempObj.nodeValue:false;
<i> </i>},
<i> </i>// Set the content of the first text node sibling of the current node
<i> </i>setText:function(node,txt){
<i> </i> if(!node.hasChildNodes()){return false;}
<i> </i> var reg=/^s+$/;
<i> </i> var tempObj=node.firstChild;
<i> </i> while(tempObj.nodeType!=3 && tempObj.nextSibling!=null || reg.test(tempObj.nodeValue)){
<i> </i> tempObj=tempObj.nextSibling;
<i> </i> }
<i> </i> if(tempObj.nodeType==3){tempObj.nodeValue=txt}else{return false;}
<i> </i>},
<i> </i>// Create a new link containing the given text
<i> </i>createLink:function(to,txt){
<i> </i> var tempObj=document.createElement('a');
<i> </i> tempObj.appendChild(document.createTextNode(txt));
<i> </i> tempObj.setAttribute('href',to);
<i> </i> return tempObj;
<i> </i>},
<i> </i>// Create a new element containing the given text
<i> </i>createTextElm:function(elm,txt){
<i> </i> var tempObj=document.createElement(elm);
<i> </i> tempObj.appendChild(document.createTextNode(txt));
<i> </i> return tempObj;
<i> </i>},
<i> </i>// Find the next or previous sibling that is an element and not a text node or line break
<i> </i>closestSibling:function(node,direction){
<i> </i> var tempObj;
<i> </i> if(direction==-1 && node.previousSibling!=null){
<i> </i> tempObj=node.previousSibling;
<i> </i> while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
<i> </i> tempObj=tempObj.previousSibling;
<i> </i> }
<i> </i> }else if(direction==1 && node.nextSibling!=null){
<i> </i> tempObj=node.nextSibling;
<i> </i> while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){
<i> </i> tempObj=tempObj.nextSibling;
<i> </i> }
<i> </i> }
<i> </i> return tempObj.nodeType==1?tempObj:false;
<i> </i>},
<i> </i>// Simulate a debugging console to avoid the need for alerts
<i> </i>initDebug:function(){
<i> </i> if(DOMhelp.debug){DOMhelp.stopDebug();}
<i> </i> DOMhelp.debug=document.createElement('div');
<i> </i> DOMhelp.debug.setAttribute('id', DOMhelp.debugWindowId);
<i> </i> document.body.insertBefore(DOMhelp.debug,document.body.firstChild);
<i> </i>},
<i> </i>setDebug:function(bug){
<i> </i> if(!DOMhelp.debug){DOMhelp.initDebug();}
<i> </i> DOMhelp.debug.innerHTML+=bug+'n';
<i> </i>},
<i> </i>stopDebug:function(){
<i> </i> if(DOMhelp.debug){
<i> </i> DOMhelp.debug.parentNode.removeChild(DOMhelp.debug);
<i> </i> DOMhelp.debug=null;
<i> </i> }
<i> </i>},
<i> </i> getKey:function(e){
<i> </i> if(window.event){
<i> </i> var key = window.event.keyCode;
<i> </i> } else if(e){
<i> </i> var key=e.keyCode;
<i> </i> }
<i> </i> return key;
<i> </i> },
<i> </i>/* helper methods */
<i> </i> getTarget:function(e){
<i> </i> var target = window.event ? window.event.srcElement : e ? e.target : null;
<i> </i> if (!target){return false;}
<i> </i> while(target.nodeType!=1 && target.nodeName.toLowerCase()!='body'){
<i> </i> target=target.parentNode;
<i> </i> }
<i> </i> return target;
<i> </i> },
<i> </i> stopBubble:function(e){
<i> </i> if(window.event && window.event.cancelBubble){
<i> </i> window.event.cancelBubble = true;
<i> </i> }
<i> </i> if (e && e.stopPropagation){
<i> </i> e.stopPropagation();
<i> </i> }
<i> </i> },
<i> </i> stopDefault:function(e){
<i> </i> if(window.event && window.event.returnValue){
<i> </i> window.event.returnValue = false;
<i> </i> }
<i> </i> if (e && e.preventDefault){
<i> </i> e.preventDefault();
<i> </i> }
<i> </i> },
<i> </i> addEvent: function(elm, evType, fn, useCapture){
<i> </i> if (elm.addEventListener){
<i> </i> elm.addEventListener(evType, fn, useCapture);
<i> </i> return true;
<i> </i> } else if (elm.attachEvent) {
<i> </i> var r = elm.attachEvent('on' + evType, fn);
<i> </i> return r;
<i> </i> } else {
<i> </i> elm['on' + evType] = fn;
<i> </i> }
<i> </i> },
<i> </i> function cssjs(a,o,c1,c2){
<i> </i> switch (a){
<i> </i> case 'swap':
<i> </i> if(!domtab.cssjs('check',o,c1)){
<i> </i> o.className.replace(c2,c1)
<i> </i> }else{
<i> </i> o.className.replace(c1,c2);
<i> </i> }
<i> </i> break;
<i> </i> case 'add':
<i> </i> if(!domtab.cssjs('check',o,c1)){
<i> </i> o.className+=o.className?' '+c1:c1;
<i> </i> }
<i> </i> break;
<i> </i> case 'remove':
<i> </i> var rep=o.className.match(' '+c1)?' '+c1:c1;
<i> </i> o.className=o.className.replace(rep,'');
<i> </i> break;
<i> </i> case 'check':
<i> </i> var found=false;
<i> </i> var temparray=o.className.split(' ');
<i> </i> for(var i=0;i<temparray.length;i++){
<i> </i> if(temparray[i]==c1){found=true;}
<i> </i> }
<i> </i> return found;
<i> </i> break;
<i> </i> }
<i> </i> },
<i> </i> safariClickFix:function(){
<i> </i> return false;
<i> </i> }
<i> </i>}
<i>
</i>&& tempObj.previousSibling!=null)
<i>
</i>tempObj=tempObj.previousSibling;
<i>
</i>return tempObj.nodeType==1?tempObj:false;
<i>
</i>tempObj=tempObj.previousSibling;
<i>
</i>return tempObj.nodeType==1?tempObj.false;
0.1.9 — BETA 5.25