if I wanted to never use innerHTML, what else would I use?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jake Barnes

    if I wanted to never use innerHTML, what else would I use?

    In the course of my research I stumbled upon this article by Alex
    Russel and Tim Scarfe:



    The case is made that innerHTML should never be used. I'm wondering, If
    I wanted all the content of BODY as a string, how else could I get
    except through innerHTML?

  • RobG

    #2
    Re: if I wanted to never use innerHTML, what else would I use?

    Jake Barnes wrote:[color=blue]
    > In the course of my research I stumbled upon this article by Alex
    > Russel and Tim Scarfe:
    >
    > http://www.developer-x.com/content/i...l/default.html
    >
    > The case is made that innerHTML should never be used.[/color]

    That's not what I understood from the article. I think it suggests that
    innerHTML has its uses but should not be abused.

    [color=blue]
    > I'm wondering, If
    > I wanted all the content of BODY as a string, how else could I get
    > except through innerHTML?[/color]

    There is the DOM 3 Load and Save module:

    "...defines the Document Object Model Load and Save Level 3, a
    platform- and language-neutral interface that allows programs and
    scripts to dynamically load the content of an XML document into a
    DOM document and serialize a DOM document into an XML document;"

    <URL:http://www.w3.org/TR/DOM-Level-3-LS/>


    Although it has reached recommendation status, it isn't widely
    implemented yet.

    You could iterate through all the nodes using DOM and build your own
    string of nodes and properties... but I expect that is totally
    impractical for anything other than trivial documents as each node has a
    large number of attributes, many of which are not explicitly set in the
    source HTML.


    --
    Rob

    Comment

    • Richard Cornford

      #3
      Re: if I wanted to never use innerHTML, what else would I use?

      Jake Barnes wrote:[color=blue]
      > In the course of my research I stumbled upon this article
      > by Alex Russel and Tim Scarfe:
      >
      > http://www.developer-x.com/content/i...l/default.html
      >
      > The case is made that innerHTML should never be used.[/color]

      That would be a stupid attitude. There is no reason to _never_ use any
      feature that any environment offers. There may be very good reasons for
      preferring to use other features, and any decision to use any given
      feature has consequences, some of which may be negative. But every
      project has a context, and some consequences vary in significance
      depending upon the context in which a feature is used.

      Ultimately what you need to be able to do is to make informed design
      decisions appropriate to the context in which you are authoring. When
      you can do that you will understand when and why not to use innerHTML,
      and when it is appropriate to use it.
      [color=blue]
      > I'm wondering, If I wanted all the content of BODY as
      > a string,[/color]

      For which the answer will depend on why you think you want the 'content
      of the BODY' as a string, what you mean by the 'content of the BODY' and
      what context you want to do this in/for. Using - innerHTML - may have
      never been appropriate for the real task to start with.
      [color=blue]
      > how else could I get
      > except through innerHTML?[/color]

      If all else fails you can serialize the DOM yourself (assuming the
      browser environment exposes it, and in sufficient detail).

      Richard.


      Comment

      • RobG

        #4
        Re: if I wanted to never use innerHTML, what else would I use?

        RobG wrote:[color=blue]
        > Jake Barnes wrote:[/color]
        [...][color=blue][color=green]
        >> I'm wondering, If
        >> I wanted all the content of BODY as a string, how else could I get
        >> except through innerHTML?[/color]
        >
        >
        > There is the DOM 3 Load and Save module:
        >
        > "...defines the Document Object Model Load and Save Level 3, a
        > platform- and language-neutral interface that allows programs and
        > scripts to dynamically load the content of an XML document into a
        > DOM document and serialize a DOM document into an XML document;"
        >
        > <URL:http://www.w3.org/TR/DOM-Level-3-LS/>
        >
        >
        > Although it has reached recommendation status, it isn't widely
        > implemented yet.[/color]

        Here's a small example that works in the latest Gecko browsers (and
        maybe IE 7 if it's implemented the required bits of DOM 3 but I doubt it):

        <script type="text/javascript">

        function showIt()
        {
        var s;
        if ( 'object' == typeof XMLSerializer
        && (s = new XMLSerializer() ) )
        {
        var pageSrc = s.serializeToSt ring(document). replace(/\</g,'&lt;');
        var oWin = window.open('', 'source','');
        oWin.document.w rite(
        '<title>Source of page<\/title><pre>'
        + pageSrc
        + '<\/pre>'
        );
        oWin.document.c lose()
        } else {
        alert('Sorry, XMLSerializer() not supported');
        }
        }

        </script>

        <input type="button" value="Show page source" onclick="showIt ();">


        --
        Rob

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: if I wanted to never use innerHTML, what else would I use?

          Jake Barnes wrote:
          [color=blue]
          > In the course of my research I stumbled upon this article by Alex
          > Russel and Tim Scarfe:
          >
          > http://www.developer-x.com/content/i...l/default.html
          >
          > The case is made that innerHTML should never be used. I'm wondering, If
          > I wanted all the content of BODY as a string, how else could I get
          > except through innerHTML?[/color]

          Making an XMLHTTP request, for example.


          PointedEars

          Comment

          • Randy Webb

            #6
            Re: if I wanted to never use innerHTML, what else would I use?

            Thomas 'PointedEars' Lahn said the following on 2/7/2006 8:35 PM:[color=blue]
            > Jake Barnes wrote:
            >[color=green]
            >> In the course of my research I stumbled upon this article by Alex
            >> Russel and Tim Scarfe:
            >>
            >> http://www.developer-x.com/content/i...l/default.html
            >>
            >> The case is made that innerHTML should never be used. I'm wondering, If
            >> I wanted all the content of BODY as a string, how else could I get
            >> except through innerHTML?[/color]
            >
            > Making an XMLHTTP request, for example.[/color]

            Given the choice between an XMLHTTP request and using innerHTML,
            innerHTML wins hands down though. As Richard pointed out, that article
            is next to useless if it says "never use innerHTML".

            --
            Randy
            comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
            Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

            Comment

            • RobG

              #7
              Re: if I wanted to never use innerHTML, what else would I use?

              Randy Webb wrote:
              [...][color=blue]
              > As Richard pointed out, that article
              > is next to useless if it says "never use innerHTML".[/color]

              It doesn't.

              It presents arguments for and against its use. Unfortunately it's more
              "he said, she said" than a reasoned explanation of when to use it or
              not, hence different readers will likely come away with different
              opinions of whether it recommends the use if innerHTML or not.

              One thing it does is to highlight that DOM 3 Load and Save does more
              than innerHTML and outerHTML combined in a more robust and supportable
              manner.

              How much support for DOM 3 will be in IE 7? It's struggling to implement
              DOM 2 fully.

              --
              Rob

              Comment

              • Jim Ley

                #8
                Re: if I wanted to never use innerHTML, what else would I use?

                On Wed, 08 Feb 2006 23:45:16 GMT, RobG <rgqld@iinet.ne t.au> wrote:
                [color=blue]
                >One thing it does is to highlight that DOM 3 Load and Save does more
                >than innerHTML and outerHTML combined in a more robust and supportable
                >manner.[/color]

                But it's pretty irrelevant given that DOM 3 L&S is defined over XML
                documents, and not HTML ones... so you're not actually comparing
                apples and apples here.
                [color=blue]
                >How much support for DOM 3 will be in IE 7? It's struggling to implement
                >DOM 2 fully.[/color]

                DOM 3 is about XML, IE7 is an HTML user agent.

                Jim.

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: if I wanted to never use innerHTML, what else would I use?

                  Jim Ley wrote:
                  [color=blue]
                  > On Wed, 08 Feb 2006 23:45:16 GMT, RobG <rgqld@iinet.ne t.au> wrote:[color=green]
                  >> One thing it does is to highlight that DOM 3 Load and Save does more
                  >> than innerHTML and outerHTML combined in a more robust and supportable
                  >> manner.[/color]
                  >
                  > But it's pretty irrelevant given that DOM 3 L&S is defined over XML
                  > documents, and not HTML ones... so you're not actually comparing
                  > apples and apples here.[/color]

                  Indeed. I missed that too, thanks.
                  [color=blue][color=green]
                  >>How much support for DOM 3 will be in IE 7? It's struggling to implement
                  >>DOM 2 fully.[/color]
                  >
                  > DOM 3 is about XML, IE7 is an HTML user agent.[/color]

                  Well, AFAIK IE 7 Final has not been released yet. Are you saying
                  that IE 7 Beta 2 still does not support application/xhtml+xml and
                  XML document types like XHTML?


                  PointedEars

                  Comment

                  • Jim Ley

                    #10
                    Re: if I wanted to never use innerHTML, what else would I use?

                    On Thu, 09 Feb 2006 17:17:24 +0100, Thomas 'PointedEars' Lahn
                    <PointedEars@we b.de> wrote:
                    [color=blue]
                    >Jim Ley wrote:[color=green][color=darkred]
                    >>>How much support for DOM 3 will be in IE 7? It's struggling to implement
                    >>>DOM 2 fully.[/color]
                    >>
                    >> DOM 3 is about XML, IE7 is an HTML user agent.[/color]
                    >
                    >Well, AFAIK IE 7 Final has not been released yet. Are you saying
                    >that IE 7 Beta 2 still does not support application/xhtml+xml and
                    >XML document types like XHTML?[/color]

                    The IE7 team are on record saying that application/xhtml+xml will not
                    be a supported type of IE7.

                    This is a good thing.

                    Jim.

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: if I wanted to never use innerHTML, what else would I use?

                      Jim Ley wrote:
                      [color=blue]
                      > [...] Thomas 'PointedEars' Lahn [...] wrote:[color=green]
                      >> Jim Ley wrote:[color=darkred]
                      >>>> How much support for DOM 3 will be in IE 7? It's struggling to
                      >>>> implement DOM 2 fully.
                      >>> DOM 3 is about XML, IE7 is an HTML user agent.[/color]
                      >> Well, AFAIK IE 7 Final has not been released yet. Are you saying
                      >> that IE 7 Beta 2 still does not support application/xhtml+xml and
                      >> XML document types like XHTML?[/color]
                      >
                      > The IE7 team are on record saying that application/xhtml+xml will
                      > not be a supported type of IE7.[/color]

                      D'oh.
                      [color=blue]
                      > This is a good thing.[/color]

                      Pardon?


                      PointedEars

                      Comment

                      • John Bokma

                        #12
                        Re: if I wanted to never use innerHTML, what else would I use?

                        Thomas 'PointedEars' Lahn <PointedEars@we b.de> wrote:
                        [color=blue]
                        > Jim Ley wrote:
                        >[color=green]
                        >> [...] Thomas 'PointedEars' Lahn [...] wrote:[color=darkred]
                        >>> Jim Ley wrote:
                        >>>>> How much support for DOM 3 will be in IE 7? It's struggling to
                        >>>>> implement DOM 2 fully.
                        >>>> DOM 3 is about XML, IE7 is an HTML user agent.
                        >>> Well, AFAIK IE 7 Final has not been released yet. Are you saying
                        >>> that IE 7 Beta 2 still does not support application/xhtml+xml and
                        >>> XML document types like XHTML?[/color]
                        >>
                        >> The IE7 team are on record saying that application/xhtml+xml will
                        >> not be a supported type of IE7.[/color]
                        >
                        > D'oh.
                        >[color=green]
                        >> This is a good thing.[/color]
                        >
                        > Pardon?[/color]

                        You really think XHTML will become mainstream? I hope not. Can you name
                        the advantages XHTML has over HTML and the disadvantages? Most people
                        currently using XHTML can't, I am afraid, list neither. I am afraid that
                        most just use XHTML because it's newer compared to HTML 4.01

                        --
                        John MexIT: http://johnbokma.com/mexit/
                        personal page: http://johnbokma.com/
                        Experienced programmer available: http://castleamber.com/
                        Happy Customers: http://castleamber.com/testimonials.html

                        Comment

                        • Jim Ley

                          #13
                          Re: if I wanted to never use innerHTML, what else would I use?

                          On Thu, 09 Feb 2006 17:39:24 +0100, Thomas 'PointedEars' Lahn
                          <PointedEars@we b.de> wrote:
                          [color=blue]
                          >Jim Ley wrote:[color=green]
                          >> The IE7 team are on record saying that application/xhtml+xml will
                          >> not be a supported type of IE7.[/color]
                          >
                          >D'oh.
                          >[color=green]
                          >> This is a good thing.[/color]
                          >
                          >Pardon?[/color]

                          The mandatory requirements of XML processing are not good for users,
                          the fail with incomprehensibl e error that Mozilla does is nothing but
                          confusing to the user.

                          This is a fundamental problem with XHTML.

                          Jim.

                          Comment

                          • Lasse Reichstein Nielsen

                            #14
                            Re: if I wanted to never use innerHTML, what else would I use?

                            jim@jibbering.c om (Jim Ley) writes:
                            [color=blue]
                            > This is a fundamental problem with XHTML.[/color]

                            The fundamental problem with XHTML is the people trying to write it
                            by hand, just as they try any other XML. That's something XML is
                            not optimized for, and was never very good at.

                            On the other hand, WYSIWYG editors for XHTML will have all the
                            problems of WYSIWYG editors for HTML, so not writing it by hand
                            won't help a lot either.

                            /L :)
                            --
                            Lasse Reichstein Nielsen - lrn@hotpop.com
                            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                            'Faith without judgement merely degrades the spirit divine.'

                            Comment

                            • Thomas 'PointedEars' Lahn

                              #15
                              Re: if I wanted to never use innerHTML, what else would I use?

                              John Bokma wrote:
                              [color=blue]
                              > Thomas 'PointedEars' Lahn <PointedEars@we b.de> wrote:[color=green]
                              >> Jim Ley wrote:[color=darkred]
                              >>> [...] Thomas 'PointedEars' Lahn [...] wrote:
                              >>>> Jim Ley wrote:
                              >>>>>> How much support for DOM 3 will be in IE 7? It's struggling to
                              >>>>>> implement DOM 2 fully.
                              >>>>> DOM 3 is about XML, IE7 is an HTML user agent.
                              >>>> Well, AFAIK IE 7 Final has not been released yet. Are you saying
                              >>>> that IE 7 Beta 2 still does not support application/xhtml+xml and
                              >>>> XML document types like XHTML?
                              >>> The IE7 team are on record saying that application/xhtml+xml will
                              >>> not be a supported type of IE7.[/color]
                              >> D'oh.
                              >>[color=darkred]
                              >>> This is a good thing.[/color]
                              >>
                              >> Pardon?[/color][/color]

                              JFTR: I think it is definitely a Bad Thing, for it keeps XHTML a corner
                              language instead of helping it to become a cornerstone language of Web
                              authoring. The contradiction and -- I must say -- hypocrisy expressed
                              by Microsoft in this matter becomes obvious when you look at the
                              wannabe-X(HT)ML code they produce (e.g. in the MSDN Library) and
                              serve that as text/html to IE's tag soup parser.
                              [color=blue]
                              > You really think XHTML will become mainstream? I hope not.[/color]

                              Yes, I hope so. But only if there is proper support in all widely
                              distributed user agents. So far that is not the case, and it is a
                              pity that it appears to stay so, 6 years after the first XHTML
                              specification to which also several Microsoft people contributed to.
                              [color=blue]
                              > Can you name the advantages XHTML has over HTML and the disadvantages?[/color]

                              Yes, I can. And once XHTML as application/xhtml+xml, parsed by an XML
                              parser, gets broad support by user agents, there are no disadvantages of
                              it left when compared to HTML. However, I have named both before (here),
                              and /this/ discussion is not on-topic and I will not continue it here.
                              [color=blue]
                              > Most people currently using XHTML can't, I am afraid, list neither.[/color]

                              That is _their_ problem.
                              [color=blue]
                              > I am afraid that most just use XHTML because it's newer compared to
                              > HTML 4.01[/color]

                              True, however that is not a valid argument against XHTML as a hopefully
                              _future_ "mainstream " markup language. And I was talking about a possible,
                              and for me desirable, future only.


                              PointedEars

                              Comment

                              Working...