HTMLDocument vs. Document when overriding write() ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Marshall

    HTMLDocument vs. Document when overriding write() ?

    I'm writing a library where I want to override document.write( ), but for
    all document objects; thus, I want to put it in the prototype. I tried

    Document.protot ype.write= my_doc_write ;

    but it didn't work. I discovered that this seemed to work:

    HTMLDocument.pr ototype.write= my_doc_write ;

    Why does HTMLDocument work here but not Document? Will this second
    version work reliably across browsers, etc.? Anything to be careful of?

    I'm unclear on the relationship between HTMLDocument and Document, which
    seems important here. Also, which of the two to use in which situations.
    Any explanations of this or appropriate links are welcome.

    Thanks!


    James
    ............... ............... ............... ............... ............... ..
    James Marshall james@jmarshall .com Berkeley, CA @}-'-,--
    "Teach people what you know."
    ............... ............... ............... ............... ............... ..

  • Lasse Reichstein Nielsen

    #2
    Re: HTMLDocument vs. Document when overriding write() ?

    James Marshall <jsm@jmarshall. com> writes:
    [color=blue]
    > I'm unclear on the relationship between HTMLDocument and Document, which
    > seems important here. Also, which of the two to use in which situations.
    > Any explanations of this or appropriate links are welcome.[/color]

    You are using Mozilla or one of the derivatives. As far as I know,
    that is the only browser that implements any of the two. I.e.,
    portability is negligable.

    On the difference between Document and HTMLDocument, they say:
    ---
    The Document interface that document implements is more general than
    HTMLDocument, which derives from it. Document is the interface that
    provides such basic hierarchy-manipulating methods and properties as
    childNodes, firstChild, nodeType, and others.
    ---
    <URL:http://www.mozilla.org/docs/dom/domref/dom_doc_ref.htm l#1027905>

    Since HTMLDocument.pr ototype contains a "write" property, it overrides
    any changes you make to Document.protot ype.write.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Richard Cornford

      #3
      Re: HTMLDocument vs. Document when overriding write() ?

      "James Marshall" <jsm@jmarshall. com> wrote in message
      news:Pine.BSF.4 .42.03101920072 30.34938-100000@jmarshal l.com...[color=blue]
      >I'm writing a library where I want to override document.write( ),
      >but for all document objects; thus, I want to put it in the
      >prototype. I tried[/color]
      [color=blue]
      > Document.protot ype.write= my_doc_write ;
      >
      > but it didn't work. I discovered that this seemed to work:
      >
      > HTMLDocument.pr ototype.write= my_doc_write ;
      >
      >Why does HTMLDocument work here but not Document?[/color]

      Without looking at the Mozilla source code (and you are testing on a
      gecko based browser) I cannot be certain that I am correct but my guess
      would be that Document is the base object for the W3C DOM _Core_
      "Document" interface, which does not have a write method, and
      HTMLDocument is the base class for the W3C _HTML_ DOM "HTMLDocume nt"
      interface, which does have a write method. HTMLDocument extends
      Document, adding properties and methods, so modifications to the
      Document prototype would be masked by the properties on the HTMLDocument
      prototype if they shared the same name. (Document would also be being
      used for XML, XSLT, etc documents).
      [color=blue]
      >Will this second version work reliably across browsers, etc.?[/color]
      <snip>

      Absolutely not. As far as I know only gecko based browsers currently
      expose the W3C interface base classes to JavaScript. And there is
      nothing in the ECMA Specifications that requires host objects to be
      amenable to modification with JavaScript anyway.

      Richard.


      Comment

      • James Marshall

        #4
        Re: HTMLDocument vs. Document when overriding write() ?

        On Mon, 20 Oct 2003, Richard Cornford wrote:

        RC> "James Marshall" <jsm@jmarshall. com> wrote
        RC> >I'm writing a library where I want to override document.write( ),
        RC> >but for all document objects; thus, I want to put it in the
        RC> >prototype. I tried
        RC>
        RC> > Document.protot ype.write= my_doc_write ;
        RC> >
        RC> > but it didn't work. I discovered that this seemed to work:
        RC> >
        RC> > HTMLDocument.pr ototype.write= my_doc_write ;
        RC> >
        <snip>
        RC> >Will this second version work reliably across browsers, etc.?
        RC> <snip>
        RC>
        RC> Absolutely not. As far as I know only gecko based browsers currently
        RC> expose the W3C interface base classes to JavaScript. And there is
        RC> nothing in the ECMA Specifications that requires host objects to be
        RC> amenable to modification with JavaScript anyway.


        Thanks Richard and Lasse for the helpful explanations. Yes, you were both
        right in that I'm using Mozilla. So now I'm wondering:

        If HTMLDocument.pr ototype.write can't be reliably overridden across
        browsers, then can Window.prototyp e.open be reliably overridden? (Or is
        that what you mean by a "host object"? I'm new to JavaScript.) It works
        in Mozilla, and I think I've seen it done in another JS library.

        If I can't override the document prototype's write() method, I'm thinking
        maybe I can override each individual document's write() method, by first
        setting "document.write = my_doc_write", and then in my_window_open( )
        setting "newwin.documen t.write= my_doc_write" . Does this seem like a
        cross-browser solution? Or do you see a better solution for overriding
        all document.write( )'s?


        Thanks again,
        James
        ............... ............... ............... ............... ............... ..
        James Marshall james@jmarshall .com Berkeley, CA @}-'-,--
        "Teach people what you know."
        ............... ............... ............... ............... ............... ..

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: HTMLDocument vs. Document when overriding write() ?

          James Marshall wrote:
          [color=blue]
          > On Mon, 20 Oct 2003, Richard Cornford wrote:
          > RC> "James Marshall" <jsm@jmarshall. com> wrote[/color]
          ^^^
          Please don't use initials in your quotes as it is confusing newsreaders
          which assume `>' to be the identificator of a quote (and therefore color
          them different). The thread and the attribution lines are enough to
          recognize who wrote what. I have cut them out here for the sake of an
          easily readable posting.
          [color=blue][color=green]
          > > [...] As far as I know only gecko based browsers currently
          > > expose the W3C interface base classes to JavaScript. And there is
          > > nothing in the ECMA Specifications that requires host objects to be
          > > amenable to modification with JavaScript anyway.[/color]
          >
          > [...]
          > If HTMLDocument.pr ototype.write can't be reliably overridden across
          > browsers, then can Window.prototyp e.open be reliably overridden?[/color]

          Overridden -- yes. Reliably -- no.
          [color=blue]
          > (Or is that what you mean by a "host object"? I'm new to JavaScript.)[/color]

          From JavaScript 1.5 on, `window' is not longer part of the core language but
          a host object which is part of the DOM of the UA (that means essentially
          that it is now officially browser-dependent.)
          [color=blue]
          > It works in Mozilla,[/color]

          Because the Gecko DOM supports that.
          [color=blue]
          > and I think I've seen it done in another JS library.[/color]

          What's your point?
          [color=blue]
          > If I can't override the document prototype's write() method, I'm thinking
          > maybe I can override each individual document's write() method, by first
          > setting "document.write = my_doc_write", and then in my_window_open( )
          > setting "newwin.documen t.write= my_doc_write" . Does this seem like a
          > cross-browser solution?[/color]

          No, not at all, since the conventions for the `window' object also apply
          to the `document' object, which in fact became(?) a property of `window'.
          [color=blue]
          > Or do you see a better solution for overriding all document.write( )'s?[/color]

          Maybe. Could you please explain what you are exactly trying to achieve?
          [color=blue]
          > ............... ............... ............... ............... ............... ..
          > James Marshall james@jmarshall .com Berkeley, CA @}-'-,--
          > "Teach people what you know."
          > ............... ............... ............... ............... ............... ..[/color]

          A signature is to be separated by `-- ' (dashDashSpace)
          which makes it being automagically removed from replies
          and colored different/hidden (if supported.)


          PointedEars

          Comment

          • HikksNotAtHome

            #6
            Re: HTMLDocument vs. Document when overriding write() ?

            In article <3F949BF8.20201 00@PointedEars. de>, Thomas 'PointedEars' Lahn
            <PointedEars@we b.de> writes:

            PointedEars>
            PointedEars>Jam es Marshall wrote:
            PointedEars>
            PointedEars>> On Mon, 20 Oct 2003, Richard Cornford wrote:
            PointedEars>> RC> "James Marshall" <jsm@jmarshall. com> wrote
            PointedEars> ^^^
            PointedEars>Ple ase don't use initials in your quotes as it is confusing
            newsreaders
            PointedEars>whi ch assume `>' to be the identificator of a quote (and therefore
            color
            PointedEars>the m different). The thread and the attribution lines are enough to
            PointedEars>rec ognize who wrote what. I have cut them out here for the sake of
            an
            PointedEars>eas ily readable posting.

            Is this comp.lang.javas cript or is it alt.how.to.post ? You seem to comment on
            every post that you reply to about the quoting methods of anybody you reply to.
            Its getting rather obnoxiously old.

            But since you are complaining about his quoting style (the RC in the quote),
            please refrain from assuming that all newsreaders will space the same way yours
            does. In mine, your ^^^^ points at the >, not the RC that you are complaining
            about.

            Since I did not use initials, then your newsreader shouldn't have problems with
            my quoting scheme. Its not the initials causing the problem but the fact that
            there are characters prior to the > in the quote.

            P.S. You also forgot to tell him to trim the header line (is that what its
            called?) and no, I won't change my way of quoting to accomodate your
            newsreader, just as I won't ask you to change your way to accomodate mine.
            --
            Randy

            Comment

            • Richard Cornford

              #7
              Re: HTMLDocument vs. Document when overriding write() ?

              "James Marshall" <jsm@jmarshall. com> wrote in message
              news:Pine.BSF.4 .42.03102014312 90.38527-100000@jmarshal l.com...
              <snip>[color=blue]
              >If HTMLDocument.pr ototype.write can't be reliably overridden
              >across browsers, then can Window.prototyp e.open be reliably
              >overridden?[/color]

              Not window.prototyp e.open because most browsers do not expose a
              prototype for the window object, but window.open can be replaced with
              another function directly (at least in all of the HTML browsers that I
              can think of). In fact that is how content inserting/re-writing proxys
              often implement their pop-up blocking abilities.
              [color=blue]
              >(Or is that what you mean by a "host object"? I'm new to
              >JavaScript.) It works in Mozilla, and I think I've seen
              >it done in another JS library.[/color]

              I mentioned ECMA Script's attitude towards host objects mostly to point
              out that while in reality DOM objects are usually quite happy to be
              modified with scripts there is no standard requiring them to. So when
              (or if) you encounter and environment that does not allow it you have no
              grounds for complaint, as the fault is the script author's for assuming
              that it would be allowed rather than the browser author's for failing to
              comply with some standard.

              JS libraries are an interesting phenomenon. There was a time when I
              thought that they were a good idea, allowing a single and consistent
              interface to the very varied environments of web browsers. As I have
              learnt more about browser scripting I have come to see that they are
              generally a very bad idea. Apart from often requiring the download of a
              fairly large chunk of JS code (of which only a tiny fraction may be
              needed for the page in question) and preventing the JS author from
              getting to grips with the problems of cross-browser scripting for
              themselves, they are usually written with an optimistic attitude towards
              their own success. That is, the author knows enough to put a consistent
              API on all of the browsers that he/she was aware of but rarely do those
              libraries make prevision for handling their own failure in the face of
              the unknown. And without the ability to fail under control, clean
              degradation becomes difficult.
              [color=blue]
              >If I can't override the document prototype's write() method,
              >I'm thinking maybe I can override each individual document's
              >write() method, by first setting "document.write = my_doc_write",[/color]

              You probably can, but what would be the point in overriding the write
              method on your own pages (and don't forget writeln)? If you have an
              alternative method then you can just call that instead of
              document.write.
              [color=blue]
              >and then in my_window_open( ) setting
              >"newwin.docume nt.write= my_doc_write" .[/color]

              That is unlikely to work. JavaScript functions are objects so
              my_doc_write is an object in the current window (it occupies memory
              their), the chances of more than a couple of browsers letting you
              execute a function from a different window as a method in their window
              is not high (framesets might be a different story), but even if it works
              as soon as the opener window is navigated or closed my_doc_write is
              gone.

              You might have slightly more success if you create a "copy" of your
              function in the new window by providing a function body string to that
              window's Function constructor:-

              newwin.document .write = new newwin.Function ( sFunctionBody );

              But attempting that in a replacement window.open function will almost
              certainly suffer from timing problems as window.open is asynchronous and
              while it returns a reference to a window object there is not guarantee
              at the point that it returns that the new window object contains a
              document object at all.
              [color=blue]
              >Does this seem like a cross-browser solution?[/color]

              I would start to judge any proposed solution by identifying the problem
              it was intended to solve. Unfortunately, I don't actually know what the
              problem this is intended to solve is.
              [color=blue]
              >Or do you see a better solution for overriding
              >all document.write( )'s?[/color]

              First establish whether the problem necessitates the overriding of
              document.write, then worry about what would be the best way to implement
              that.

              Richard.


              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: HTMLDocument vs. Document when overriding write() ?

                HikksNotAtHome wrote:
                [color=blue]
                > [...]
                > Is this comp.lang.javas cript or is it alt.how.to.post ? You seem to comment on
                > every post that you reply to about the quoting methods of anybody you reply to.
                > Its getting rather obnoxiously old.[/color]

                In contrast to your posting, mine was at least on-topic for
                the most part. Grab your own nose.

                And if you don't want to post so that your postings are for
                your readers, why the hell are you posting in the first place?
                [color=blue]
                > PointedEars>
                > PointedEars>Jam es Marshall wrote:
                > [aso.][/color]

                If it was not obvious that names/initials in every line
                of quote destroys readability, it should be obvious now.


                PointedEars

                Comment

                • Richard Cornford

                  #9
                  Re: HTMLDocument vs. Document when overriding write() ?

                  "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
                  news:bn2vhq$pk$ 1$8302bc10@news .demon.co.uk...
                  <snip>[color=blue]
                  > ... , but window.open can be replaced with another function
                  > directly (at least in all of the HTML browsers that I
                  > can think of). ...[/color]
                  <snip>

                  I obviously wasn't thinking that clearly when I wrote that as there are
                  no shortage of small PDA and embedded HTML browsers that do not have a
                  window.open function to start with, and you cannot replace what doesn't
                  exist.

                  Richard.


                  Comment

                  • Richard Cornford

                    #10
                    Re: HTMLDocument vs. Document when overriding write() ?

                    "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
                    news:3F95270D.2 040102@PointedE ars.de...[color=blue][color=green]
                    >> [...]
                    >>Is this comp.lang.javas cript or is it alt.how.to.post ? You
                    >>seem to comment on every post that you reply to about the
                    >>quoting methods of anybody you reply to. Its getting rather
                    >>obnoxiously old.[/color]
                    >
                    >In contrast to your posting, mine was at least on-topic for
                    >the most part. Grab your own nose.
                    >
                    >And if you don't want to post so that your postings are for
                    >your readers, why the hell are you posting in the first place?
                    >[color=green]
                    >>PointedEars >
                    >>PointedEars>J ames Marshall wrote:
                    >>[aso.][/color]
                    >
                    >If it was not obvious that names/initials in every line
                    >of quote destroys readability, it should be obvious now.[/color]

                    Ultimately the acceptable posting style for the group is outlined in the
                    FAQ (section 2.3) and the resources that it references. And given that
                    the FAQ does prescribe a posting style for this group it seems
                    inappropriate to be proposing a more restrictive style to users of the
                    group, especially when some of those restrictions appear to be based on
                    nothing other than your personal opinion and perceptions.

                    You are of course free to lobby for modifications to section 2.3 (as per
                    section 5.2) to bring it into line with your particular interpretation
                    of an appropriate style for Usenet postings, and I am sure that if your
                    proposed changes met with a consensus of approval (or even a majority of
                    expressed opinions) Jim would happily modify the FAQ accordingly.

                    Richard.


                    Comment

                    Working...