Beginning Javascript with DOM Scripting and AJAX
In my lifetime I can honestly say that I have only bought two books on JavaScript programming. The first one was called "Special Edition: Using JavaScript" [Que-1996]. This was basically a reference book that described, and provided an example of, every function and method in JavaScript at that time. I don't think I have opened this book in about 8 years.
During this time, I have relied heavily on Google to provide information on any new tips and tricks that have come out. If I came across something that intrigued me, I would spend countless hours learning all I could from the internet.
Recently I purchased a new book called "Beginning Javascript with DOM Scripting and AJAX" [Apress-2006]. I primarily bought this to enhance my knowledge of DOM programming, but it also enlightened me to some new techniques that I've seen, but I really never fully understood.
So, if you need a refresher course or if you are just starting out in JavaScript programming, I highly recommend checking this out. It's well written and very easy to understand. The author provides a lot of code samples, which you can download from his site.
The one thing I would like to point out, is that he provides a few DOM functions in a DOM Utility Script. These functions are called: lastSibling, firstSibling and closestSibling. These are extremely useful, but I don't like how he's returning the value.
Here are the functions:
var DOMhelp={
lastSibling:function(node){
var tempObj=node.parentNode.lastChild;
while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
tempObj=tempObj.previousSibling;
}
return (tempObj.nodeType==1)?tempObj:false;
},
firstSibling:function(node){
var tempObj=node.parentNode.firstChild;
while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){
tempObj=tempObj.nextSibling;
}
return (tempObj.nodeType==1)?tempObj:false;
},
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 have a problem with the way he's returning the values:
return tempObj.nodeType == 1 ? tempObj: false;
What I don't like about this method is that he doesn't test to see if the tempObj is a valid object. Also, he's returning false. I don't think that this follows the standard form in which javascript returns results for objects. I think it should return "undefined" if it's not a valid DOM object. So, here's what I recommend changing the return value to:
return (tempObj && tempObj.nodeType == 1) ? tempObj: "undefined";
Still the book is a good read and his other examples are very nice. Check it out and let me know what you think.
Have something to say? Leave a comment!
You don't have to be registered to leave a comment. Unregistered user's comments will be approved before going live.
You are currently posting as an unregistered user.
This means that your comment will be reviewed prior to going live. If you are a registered user, please login. New user? No problem, register for an account, it's FREE! Benefits include, posting instantly, screen name protection, collaboration recognition, subscribe to article updates, and so much more!