Most people agree, that creating textNodes is the correct way to handle
adding text to a DOM node. Other than that here are the arguments: -
Using a text node: It lets you add text w/o
clobbering other child nodes. -
Using innerHTML: you destroy all previous child nodes
and text. -
Using innerHTML: you could use innerHTML += “my text”
to append text as well and that will just create a new text node for you. -
Using a text node : you can insert your new node
before, after, or between other DOM nodes -
Using innerHTML is faster than creating new nodes -
Using innerHTML: will let you put invalid HTML on
your page via scripts (the biggest argument against) And here is the kicker: -
In IE only: if you use innerHTML there is a 1ms-100ms
lag between the DOM tree/DOM nodes being updated with your new HTML. However it
is instantly visible to the user. Meaning, if you set the innerHTML of an element to
HTML that will create other DOM nodes, you will not be able to do
element.childNodes[] right away. There will be a time lag before the DOM is
updated. This only matters in time sensitive code where lots of objects and
data is moving and changing. I found it by chance and it has only ever affect
one of my scripts in years of work. Again, this only happens in IE. It was 2 days of intense pain tracking down a bug in
some really complex JS code that was time sensitive that lead me to the above… Other than that is it preference. I personally only
use innerHTML when the text node is the only thing being put in an element. If
I am putting multiple separate elements or trying to preserve previous child
elements, I use nodes. -Andrew Martinez -----Original
Message----- Lets go to www.google.com In the URL/address bar, clear any text there. Type: _javascript_:document.write(‘omg where did google
go’ ); Press enter. No onload, but google is now gone! ---------------------------------------------------------- Thanks.
One last question to clean up... oMyDiv
= document.createElement('div'); document.body.appendChild(oMyDiv); sMyText
= 'Howdy! This is western text!'; oMyDiv.innerHTML
= sMyText; oMyText
= document.createTextNode(sMyText); oMyDiv.appendChild(oMyText); Does
one method of adding innerHTML have benefits / features that the other does
not? Sam |
_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs