emulating document.documentElement on pre W3C DOM browsers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aaron Gray

    emulating document.documentElement on pre W3C DOM browsers


    (Please tell me if this is silly or I am barking up the wrong tree)

    Can someone check this please on pre W3C DOM browsers :-

    function getDocumentRoot ()
    {
    for ( e in document.childN odes)
    if ( document.childN odes[e].nodeName == "HTML")
    return document.childN odes[e];
    return document.body;
    }

    if (!document.docu mentElement)
    document.docume ntElement = getDocumentRoot ();

    There's an html test page here :-



    There's also a test for document.getEle mentById() emulation here :-



    Many thanks in advance,

    Aaron


  • Aaron Gray

    #2
    Re: emulating document.docume ntElement on pre W3C DOM browsers

    "Aaron Gray" <ang.usenet@gma il.comwrote in message
    news:6dpokbF3n7 jaU1@mid.indivi dual.net...
    if (!document.docu mentElement)
    document.docume ntElement = getDocumentRoot ();
    Minor behavioural amendment :-

    if (!document.docu mentElement)
    {
    document.docume ntElement = getDocumentRoot ();
    document.docume ntElement.readO nly = true;
    }

    Aaron



    Comment

    • SAM

      #3
      Re: emulating document.docume ntElement on pre W3C DOM browsers

      Aaron Gray a écrit :
      "Aaron Gray" <ang.usenet@gma il.comwrote in message
      news:6dpokbF3n7 jaU1@mid.indivi dual.net...
      Your page-test works well with my Fx.3
      (while it is not W3C compliant)
      > if (!document.docu mentElement)
      > document.docume ntElement = getDocumentRoot ();
      >
      Minor behavioural amendment :-
      >
      if (!document.docu mentElement)
      {
      document.docume ntElement = getDocumentRoot ();
      on my idea the next line will be not used.

      'HTML' has been written on the page
      so, probably, there is no more existing JS.
      document.docume ntElement.readO nly = true;
      }
      if (!document.docu mentElement)
      {
      document.docume ntElement = getDocumentRoot ();
      document.docume ntElement.readO nly = true;
      alert('vu');
      }

      the alert doesn't fire

      --
      sm

      Comment

      • Aaron Gray

        #4
        Re: emulating document.docume ntElement on pre W3C DOM browsers

        "SAM" <stephanemoriau x.NoAdmin@wanad oo.fr.invalidwr ote in message
        news:4877e3a3$0 $925$ba4acef3@n ews.orange.fr.. .
        Aaron Gray a écrit :
        >"Aaron Gray" <ang.usenet@gma il.comwrote in message
        >news:6dpokbF3n 7jaU1@mid.indiv idual.net...
        >
        Your page-test works well with my Fx.3
        (while it is not W3C compliant)
        >
        >> if (!document.docu mentElement)
        >> document.docume ntElement = getDocumentRoot ();
        >>
        >Minor behavioural amendment :-
        >>
        > if (!document.docu mentElement)
        > {
        > document.docume ntElement = getDocumentRoot ();
        >
        on my idea the next line will be not used.
        >
        'HTML' has been written on the page
        so, probably, there is no more existing JS.
        Great.
        > document.docume ntElement.readO nly = true;
        > }
        >
        if (!document.docu mentElement)
        {
        document.docume ntElement = getDocumentRoot ();
        document.docume ntElement.readO nly = true;
        alert('vu');
        }
        >
        the alert doesn't fire
        Okay Fx.3 does not support the readOnly attribute then.

        Thats annoying as AFAICS theres no real fix for that :(

        Aaron


        Comment

        • dhtml

          #5
          Re: emulating document.docume ntElement on pre W3C DOM browsers

          On Jul 11, 11:55 am, "Aaron Gray" <ang.use...@gma il.comwrote:
          (Please tell me if this is silly or I am barking up the wrong tree)
          >
          Can someone check this please on pre W3C DOM browsers :-
          >
          Don't do this:
              {
                  for ( e in document.childN odes)
                      if ( document.childN odes[e].nodeName == "HTML")
                          return document.childN odes[e];
                  return document.body;
              }
          >

          for in enumerates and does this up the prototype chain. In Firefox,
          "item" will be enumerated, e.g document.childN odes['item']. This is
          inefficient and undesirable.

          >
          Aaron

          Comment

          • Aaron Gray

            #6
            Re: emulating document.docume ntElement on pre W3C DOM browsers

            "dhtml" <dhtmlkitchen@g mail.comwrote in message
            news:7884bfaa-e35f-4015-a87e-18e26137ffa4@l6 4g2000hse.googl egroups.com...
            On Jul 11, 11:55 am, "Aaron Gray" <ang.use...@gma il.comwrote:
            >(Please tell me if this is silly or I am barking up the wrong tree)
            >>
            >Can someone check this please on pre W3C DOM browsers :-
            >>
            >Don't do this:
            >{
            >for ( e in document.childN odes)
            >if ( document.childN odes[e].nodeName == "HTML")
            >return document.childN odes[e];
            >return document.body;
            >}
            >>
            >for in enumerates and does this up the prototype chain. In Firefox,
            >"item" will be enumerated, e.g document.childN odes['item']. This is
            >inefficient and undesirable.
            Okay so I use Array iteration rather than object iteration, thus :-

            for ( var i = 0, N = document.childN odes.length; i < N; ++i)
            if ( document.childN odes[i].nodeName == "HTML")
            return document.childN odes[i];
            return document.body;

            I just learned that lesson else where :)

            Thanks,

            Aaron


            Comment

            • Richard Cornford

              #7
              Re: emulating document.docume ntElement on pre W3C DOM browsers

              Aaron Gray wrote:
              >
              (Please tell me if this is silly or I am barking up the
              wrong tree)
              Yes and Yes.
              Can someone check this please on pre W3C DOM browsers :-
              There is no need because Netscape 4 and IE 4 have no - childNodes -
              collections/nodeLists. IE 4 had - children - collections, but not on the
              document object if I recall correctly) and Netscape 4 had no means of
              accessing an HTML element (because it could not be made into a layer
              with CSS).
              function getDocumentRoot ()
              {
              for ( e in document.childN odes)
              The for-in statement throws and exception if the expression on the right
              of the - in - is not type-convertible into an object, so IE 4 and
              Netscape 4 exit here.
              if ( document.childN odes[e].nodeName == "HTML")
              return document.childN odes[e];
              return document.body;
              Netscape 4 has no - docuemnt.body - either.
              }
              <snip>

              Richard.

              Comment

              • Aaron Gray

                #8
                Re: emulating document.docume ntElement on pre W3C DOM browsers

                "Richard Cornford" <Richard@litote s.demon.co.ukwr ote in message
                news:g59201$do1 $1$8300dec7@new s.demon.co.uk.. .
                Aaron Gray wrote:
                >>
                >(Please tell me if this is silly or I am barking up the
                >wrong tree)
                >
                Yes and Yes.
                Right :)
                >Can someone check this please on pre W3C DOM browsers :-
                >
                There is no need because Netscape 4 and IE 4 have no - childNodes -
                collections/nodeLists. IE 4 had - children - collections, but not on the
                document object if I recall correctly) and Netscape 4 had no means of
                accessing an HTML element (because it could not be made into a layer with
                CSS).
                >
                > function getDocumentRoot ()
                > {
                > for ( e in document.childN odes)
                >
                The for-in statement throws and exception if the expression on the right
                of the - in - is not type-convertible into an object, so IE 4 and Netscape
                4 exit here.
                Okay that has been moded to an array indexing for loop.
                > if ( document.childN odes[e].nodeName == "HTML")
                > return document.childN odes[e];
                > return document.body;
                >
                Netscape 4 has no - docuemnt.body - either.
                Right
                > }
                <snip>
                >
                Richard.
                Great, thats cut the lower end off at IE4 and NN4.

                Thanks,

                Aaron


                Comment

                • Gregor Kofler

                  #9
                  Re: emulating document.docume ntElement on pre W3C DOM browsers

                  Aaron Gray meinte:
                  Okay so I use Array iteration rather than object iteration, thus :-
                  >
                  for ( var i = 0, N = document.childN odes.length; i < N; ++i)
                  if ( document.childN odes[i].nodeName == "HTML")
                  return document.childN odes[i];
                  return document.body;
                  >
                  I just learned that lesson else where :)
                  Ok. Now add curly brackets - and it looks more like JavaScript ("N"
                  should become "n", too). You know JSLint [1]?

                  Gregor

                  [1]
                  JSLint, The JavaScript Code Quality and Coverage Tool. This file allows JSLint to be run from a web browser. It can accept a source program and analyze it without sending it over the network.



                  --
                  http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
                  http://web.gregorkofler.com ::: meine JS-Spielwiese
                  http://www.image2d.com ::: Bildagentur für den alpinen Raum

                  Comment

                  • Aaron Gray

                    #10
                    Re: emulating document.docume ntElement on pre W3C DOM browsers

                    "Gregor Kofler" <usenet@gregork ofler.atwrote in message
                    news:aEbek.83$1 l4.36@nntpserve r.swip.net...
                    Aaron Gray meinte:
                    >
                    >Okay so I use Array iteration rather than object iteration, thus :-
                    >>
                    > for ( var i = 0, N = document.childN odes.length; i < N; ++i)
                    > if ( document.childN odes[i].nodeName == "HTML")
                    > return document.childN odes[i];
                    > return document.body;
                    >>
                    >I just learned that lesson else where :)
                    >
                    Ok. Now add curly brackets - and it looks more like JavaScript ("N" should
                    become "n", too). You know JSLint [1]?
                    >
                    Gregor
                    >
                    [1]
                    http://www.jslint.com/
                    Thanks, have not got to Lint'ing things yet. I did not like that N either :)

                    Aaron


                    Comment

                    • Richard Cornford

                      #11
                      Re: emulating document.docume ntElement on pre W3C DOM browsers

                      Aaron Gray wrote:
                      Richard Cornford wrote:
                      >Aaron Gray wrote:
                      <snip>
                      >>Can someone check this please on pre W3C DOM browsers :-
                      <snip>
                      >> function getDocumentRoot ()
                      >> {
                      >> for ( e in document.childN odes)
                      <snip>
                      >... IE 4 and Netscape 4 exit here.
                      <snip>
                      Great, thats cut the lower end off at IE4 and NN4.
                      So which "pre W3C DOM browsers" come after IE 4 and NN4? I suppose
                      Konqueror 2 might have fallen into that gap, but its "experiment al"
                      script engine was so far short of any ECMAScript specification that it
                      could hardly execute scripts at all (and certainly nothing modern (not
                      having support for, for example, function expressions)).

                      Richard.

                      Comment

                      • SAM

                        #12
                        Re: emulating document.docume ntElement on pre W3C DOM browsers

                        Gregor Kofler a écrit :JSLint is completely mad if not crazy !
                        Only trying to follow how it thinks indentations have to be
                        gives a code unreadable for me.


                        --
                        sm


                        Comment

                        • Aaron Gray

                          #13
                          Re: emulating document.docume ntElement on pre W3C DOM browsers

                          "Richard Cornford" <Richard@litote s.demon.co.ukwr ote in message
                          news:g5bka9$rk5 $1$8300dec7@new s.demon.co.uk.. .
                          Aaron Gray wrote:
                          >Richard Cornford wrote:
                          >>Aaron Gray wrote:
                          <snip>
                          >>>Can someone check this please on pre W3C DOM browsers :-
                          <snip>
                          >>> function getDocumentRoot ()
                          >>> {
                          >>> for ( e in document.childN odes)
                          <snip>
                          >>... IE 4 and Netscape 4 exit here.
                          <snip>
                          >Great, thats cut the lower end off at IE4 and NN4.
                          >
                          So which "pre W3C DOM browsers" come after IE 4 and NN4? I suppose
                          Konqueror 2 might have fallen into that gap, but its "experiment al" script
                          engine was so far short of any ECMAScript specification that it could
                          hardly execute scripts at all (and certainly nothing modern (not having
                          support for, for example, function expressions)).
                          Thanks Richard, that hopefully just about wraps that one up then.

                          Aaron


                          Comment

                          • Aaron Gray

                            #14
                            Re: emulating document.docume ntElement on pre W3C DOM browsers

                            "SAM" <stephanemoriau x.NoAdmin@wanad oo.fr.invalidwr ote in message
                            news:487954e3$0 $884$ba4acef3@n ews.orange.fr.. .
                            Gregor Kofler a écrit :>
                            JSLint is completely mad if not crazy !
                            Only trying to follow how it thinks indentations have to be
                            gives a code unreadable for me.
                            Oh! Have not had chance to try it out yet ! Could be fun, or maybe not fun
                            :)

                            What indenting scheme do you use ?

                            Aaron


                            Comment

                            • Gregor Kofler

                              #15
                              Re: emulating document.docume ntElement on pre W3C DOM browsers

                              SAM meinte:
                              Gregor Kofler a écrit :>
                              JSLint is completely mad if not crazy !
                              It isn't. It's perfect for tracking down those otherwise extremely hard
                              to find bugs.
                              Only trying to follow how it thinks indentations have to be
                              gives a code unreadable for me.
                              What indentations? JSLint normally offers explanations why to follow
                              certain practices and guidelines. My code stays perfectly readable - in
                              fact I didn't have to change anything of my usual formatting.

                              Gregor


                              --
                              http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
                              http://web.gregorkofler.com ::: meine JS-Spielwiese
                              http://www.image2d.com ::: Bildagentur für den alpinen Raum

                              Comment

                              Working...