can't get away from innerHTML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralph Snart

    can't get away from innerHTML

    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.getEle mentById('mydiv ').innerHTML='i de&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.create TextNode("ide&e acute; fixe");
    var oldNode = document.getEle mentById('mydiv ').firstChild;
    var removedNode = document.getEle mentById('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-
  • Douglas Crockford

    #2
    Re: can't get away from innerHTML

    > personally i don't have any problems with innerHTML, despite it not being a[color=blue]
    > 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.[/color]

    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.parseH TML(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.


    Comment

    • Michael Winter

      #3
      Re: can't get away from innerHTML

      On Wed, 05 Jan 2005 01:19:10 GMT, Ralph Snart <snart@nospam.c om> wrote:
      [color=blue]
      > i've read that innerHTML is not in the W3C standards and never will be.[/color]

      Correct[1].

      [snip]
      [color=blue]
      > (spaces inserted so your newsreader won't transform it)[/color]

      It won't matter as you should be (and are) posting in plain text.
      [color=blue]
      > 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.[/color]

      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.

      Comment

      • Martin Honnen

        #4
        Re: can't get away from innerHTML



        Ralph Snart wrote:

        [color=blue]
        > <div id="mydiv"></div>
        > <script type="text/javascript">
        > document.getEle mentById('mydiv ').innerHTML='i de&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.create TextNode("ide&e acute; fixe");
        > var oldNode = document.getEle mentById('mydiv ').firstChild;
        > var removedNode = document.getEle mentById('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)[/color]

        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.create TextNode("ideƩ" )
        If you want to rely on JavaScript to encode the character then you can use
        document.create TextNode("ide" + String.fromChar Code(233))
        or
        document.create TextNode("ide\u 00E9")

        --

        Martin Honnen

        Comment

        Working...