10 September 2005 15 comments Web development
This blog post is 17 years old! Most likely, its content is outdated. Especially if it's technical.
document.getElementById('msg').firstChild.nodeValue = message;
Inside the document there's a span tag that this writes to:
Why use this arcane
firstChild.nodeValue when you can use
document.getElementById('msg').innerHTML = message;
- Announcing Smurl - a free URL compressor 7 September 2005
- Misstake or hidden Nazi message? 13 September 2005
- Related by category:
- Fastest way to find out if a file exists in S3 (with boto3) 16 June 2017 Web development
- How to throttle AND debounce an autocomplete input in React 1 March 2018 Web development
- How to create-react-app with Docker 17 November 2017 Web development
- Be very careful with your add_header in Nginx! You might make your site insecure 11 February 2018 Web development
- How to have default/initial values in a Django form that is bound and rendered 10 January 2020 Web development
- Related by keyword:
Scroll down to "Arguments for and against" for the precis. But it appears to boil down to "DOM is hard to use and powerful. InnerHTML is easy to use, widely implemented but non-standard. Your call."
thanks djo. I feel compelled to go back to firstChild.nodeValue but innerHTML seems a much better solution (even though it's "wrong") because I like things simple.
document.getElementById("msg").innerHTML = "<div style='margin: 10px; padding: 20px;'><i>Text</i></div>";
document.getElementById("msg1").firstChild.nodeValue = "<div style='margin: 10px; padding: 20px;'><i>Text</i></div>";
<a target="firefox" href="http://validator.w3.org/check?uri=referer">checkme with validator...!</a>
I see! It doesn't work.
The answer to your initial question about how the .firstChild.nodeValue code could have ever worked with the span tag you give is that it will work in Mozilla Firefox (tested on v1.5) if there is a space inside the span tag:
<span id="msg">(space here)</span>
document.getElementById("msg").firstChild has no properties
Unfortunately, adding just a space still gives an error in IE 6 so the true answer is that needs to be added. Maybe the old code was cleaned up and some spaces or 's were removed? Without something inside the span tag, there's no text node so .firstChild.nodeValue doesn't exist.
PS I hope you don't mind me posting to something this old, but this page places very high for a Google search on ".firstChild.nodeValue" so I thought some more comments might help anyone else who stumbles across it.
someone help me how to pass anchor text to next page using session
x = document.createTextNode('test');
Works both in FF and IE.
Brazilian ICT Offshore
Unfortunately, innerHTML, textContent & firstChild.nodeValue don't preserve any extra white spaces in the same way that innerText does. I'm still scratch'n, so if anyone knows a way I'm all ears.
val = msg.firstChild;
actualValue = val.data;
using innerHtml can cause errors in some older browsers because it's not offical.
screw older browsers.
Inner HTML or FirstChild?
There can also be a fairly large performance difference. Performance of the .firstNode.nodeValue method can be several times faster than the .innerHTML method, because assignment to firstNode.nodeValue makes no attempt to parse any potential HTML or make any DOM changes. (In fact the .firstNode.nodeValue method doesn't even notice embedded HTML, and so will misbehave if given anything more than just a simple text string).
Whether or not the performance of just this one operation significantly impacts the overall performance of the webpage, and whether or not performance is an issue at all, and whether or not this tradeoff of performance for clarity is considered a good deal, are all of course different for every situation. About all one can say is YMMV.
how come we only get the line <div style='margin: 10px; padding: 20px;'><i>Text</i></div> for document.getElementById("msg1").firstChild.nodeValue
textContent vs innerHTML
innerHTML returns the HTML as its name indicates. Quite often, in order to retrieve or write text within an element, people use innerHTML. textContent should be used instead. Because the text is not parsed as HTML, it's likely to have better performance.
Moreover, this avoids an XSS attack vector!
-> use msg.firstChild.data