can't get away from innerHTML

Discussion in 'Javascript' started by Ralph Snart, Jan 5, 2005.

  1. Ralph Snart

    Ralph Snart Guest

    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-
     
    Ralph Snart, Jan 5, 2005
    #1
    1. Advertising

  2. > 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
     
    Douglas Crockford, Jan 5, 2005
    #2
    1. Advertising

  3. On Wed, 05 Jan 2005 01:19:10 GMT, Ralph Snart <> wrote:

    > 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.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Jan 5, 2005
    #3
  4. 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")

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 5, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Klauer

    Can one get away with an under-allocated union?

    Alexander Klauer, Dec 25, 2010, in forum: C Programming
    Replies:
    5
    Views:
    599
    Eric Sosman
    Dec 26, 2010
  2. Jake Barnes
    Replies:
    2
    Views:
    129
    Jake Barnes
    Apr 7, 2006
  3. sonic
    Replies:
    5
    Views:
    294
    Randy Webb
    Jul 11, 2006
  4. Jake Barnes
    Replies:
    3
    Views:
    162
    Jake Barnes
    Sep 10, 2007
  5. Ian Rastall
    Replies:
    7
    Views:
    481
Loading...

Share This Page