SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 238 | Next

Richard Wagner

"Professional iPhone and iPod touch Programming: Building Applications for Mobile Safari"

For example, compare the following:
// Less efficient
var l1=document.createTextNode(???Line 1??™);
var l2=document.createTextNode(???Line 2??™);
// More efficient
var d=document;
var l1=d.createTextNode(???Line 1??™);
var l2=d.createTextNode(???Line 2??™);
If you reference document a handful of times, then it is probably not practical to go through this trouble.
But if you find yourself writing document a thousand times in your code, the efficiency gains make this
practice a definite consideration.
Offline DOM Manipulation
When you are writing to the DOM, assemble your subtree of nodes outside of the actual DOM, and then
insert the subtree once at the end of the process. For example, consider the following:
var comments=customBlog.getComments(???index??™);
var c=comments.count;
var entry;
var commentDiv = document.createElement(???div??™);
document.body.appendChild(commentDiv);
for (var i=0;i < c;i++) {
entry=document.createElement(???p??™);
entry.appendChild( document.createTextNode(comments[i]);
commentDiv.appendChild( entry );
}
Consider the placement of the grayed, highlighted line. Because you add the new div element to the
DOM before you add children to it, the document must be updated for each new paragraph added.


Pages:
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250