can't get away from innerHTML

R

Ralph Snart

i've read that innerHTML is not in the W3C standards and never will be.
however i've found something that i simply can't do by manipulating
text nodes directly.

<div id="mydiv"></div>
<script type="text/javascript">
document.getElementById('mydiv').innerHTML='ide&eacute; fixe';
</script>

this prints "idee fixe" with the final 'e' of idee having an acute
accent (the &eacute; entity).

however if i use this javascript instead:

var newNode = document.createTextNode("ide&eacute; fixe");
var oldNode = document.getElementById('mydiv').firstChild;
var removedNode = document.getElementById('mydiv').replaceChild(newNode, oldNode);

this does not display an accented e. it displays the literal string & e a c u t e ;
(spaces inserted so your newsreader won't transform it)

personally i don't have any problems with innerHTML, despite it not being a
standard. however, i'd like to know for my own curiosity if there's a way
to get the innerHTML behavior with the DOM-approved node manipulation commands.


-rs-
 
D

Douglas Crockford

personally i don't have any problems with innerHTML, despite it not being a
standard. however, i'd like to know for my own curiosity if there's a way
to get the innerHTML behavior with the DOM-approved node manipulation commands.

innerHTML exposes the HTML parser, which is a very handle tool to have.
Lacking it, you would have to do all the HTML parsing yourself, which is
a bad thing.

I think a better interface would have been to make the calling of the
parser explicit, like document.parseHTML(source). I don't like innerHTML
because I think that assignment should not have side-effects.

There are lots of features in the browsers which are useful and
necessary, and not specified in any standard. I think that is an
indication of the quality and completeness of the WWW standards.

http://www.JSON.org
 
M

Michael Winter

i've read that innerHTML is not in the W3C standards and never will be.
Correct[1].

[snip]

(spaces inserted so your newsreader won't transform it)

It won't matter as you should be (and are) posting in plain text.
however, i'd like to know for my own curiosity if there's a way to
get the innerHTML behavior with the DOM-approved node manipulation
commands.

You'll have to specify the Unicode code point value (\u00e9). Character
references are within the domain of HTML, not client-side scripts. As the
HTML parser skips the content of SCRIPT elements, no translation occurs so
the script parser will still encounter the character reference and treat
it as literal text.

The exception is within intrinsic events. In this case, the HTML parser
*does* handle the content so character references will be translated. When
the script parser takes over later, it will find the actual character, not
the reference.

Mike


[1] Well, I couldn't guarantee that the second part is true, but it's
highly unlikely that innerHTML will ever be added to the W3C DOM.
 
M

Martin Honnen

Ralph Snart wrote:

<div id="mydiv"></div>
<script type="text/javascript">
document.getElementById('mydiv').innerHTML='ide&eacute; fixe';
</script>

this prints "idee fixe" with the final 'e' of idee having an acute
accent (the &eacute; entity).

however if i use this javascript instead:

var newNode = document.createTextNode("ide&eacute; fixe");
var oldNode = document.getElementById('mydiv').firstChild;
var removedNode = document.getElementById('mydiv').replaceChild(newNode, oldNode);

this does not display an accented e. it displays the literal string & e a c u t e ;
(spaces inserted so your newsreader won't transform it)

Of course, the DOM operates on utf-16 encoded strings, it has no idea
about HTML character references like &eacute;, but all you need to do is
use the appropriate character in a properly encoded documented e.g.
document.createTextNode("ideé")
If you want to rely on JavaScript to encode the character then you can use
document.createTextNode("ide" + String.fromCharCode(233))
or
document.createTextNode("ide\u00E9")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top