By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,607 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,607 IT Pros & Developers. It's quick & easy.

HTMLDocument vs. Document when overriding write() ?

P: n/a
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.prototype.write= my_doc_write ;

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

HTMLDocument.prototype.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 ja***@jmarshall.com Berkeley, CA @}-'-,--
"Teach people what you know."
.................................................. ...........................

Jul 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
James Marshall <js*@jmarshall.com> writes:
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.


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.html#1027905>

Since HTMLDocument.prototype contains a "write" property, it overrides
any changes you make to Document.prototype.write.

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

P: n/a
"James Marshall" <js*@jmarshall.com> wrote in message
news:Pi**************************************@jmar shall.com...
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.prototype.write= my_doc_write ;

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

HTMLDocument.prototype.write= my_doc_write ;

Why does HTMLDocument work here but not Document?
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 "HTMLDocument"
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).
Will this second version work reliably across browsers, etc.?

<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.
Jul 20 '05 #3

P: n/a
On Mon, 20 Oct 2003, Richard Cornford wrote:

RC> "James Marshall" <js*@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.prototype.write= my_doc_write ;
RC> >
RC> > but it didn't work. I discovered that this seemed to work:
RC> >
RC> > HTMLDocument.prototype.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.prototype.write can't be reliably overridden across
browsers, then can Window.prototype.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.document.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 ja***@jmarshall.com Berkeley, CA @}-'-,--
"Teach people what you know."
.................................................. ...........................

Jul 20 '05 #4

P: n/a
James Marshall wrote:
On Mon, 20 Oct 2003, Richard Cornford wrote:
RC> "James Marshall" <js*@jmarshall.com> wrote ^^^
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.
[...] 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.


[...]
If HTMLDocument.prototype.write can't be reliably overridden across
browsers, then can Window.prototype.open be reliably overridden?


Overridden -- yes. Reliably -- no.
(Or is that what you mean by a "host object"? I'm new to JavaScript.)
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.)
It works in Mozilla,
Because the Gecko DOM supports that.
and I think I've seen it done in another JS library.
What's your point?
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.document.write= my_doc_write" . Does this seem like a
cross-browser solution?
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'.
Or do you see a better solution for overriding all document.write()'s?
Maybe. Could you please explain what you are exactly trying to achieve?
.................................................. ...........................
James Marshall ja***@jmarshall.com Berkeley, CA @}-'-,--
"Teach people what you know."
.................................................. ...........................


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

Jul 20 '05 #5

P: n/a
In article <3F**************@PointedEars.de>, Thomas 'PointedEars' Lahn
<Po*********@web.de> writes:

PointedEars>
PointedEars>James Marshall wrote:
PointedEars>
PointedEars>> On Mon, 20 Oct 2003, Richard Cornford wrote:
PointedEars>> RC> "James Marshall" <js*@jmarshall.com> wrote
PointedEars> ^^^
PointedEars>Please don't use initials in your quotes as it is confusing
newsreaders
PointedEars>which assume `>' to be the identificator of a quote (and therefore
color
PointedEars>them different). The thread and the attribution lines are enough to
PointedEars>recognize who wrote what. I have cut them out here for the sake of
an
PointedEars>easily readable posting.

Is this comp.lang.javascript 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
Jul 20 '05 #6

P: n/a
"James Marshall" <js*@jmarshall.com> wrote in message
news:Pi**************************************@jmar shall.com...
<snip>
If HTMLDocument.prototype.write can't be reliably overridden
across browsers, then can Window.prototype.open be reliably
overridden?
Not window.prototype.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.
(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.
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.
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",
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.
and then in my_window_open() setting
"newwin.document.write= my_doc_write" .
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.
Does this seem like a cross-browser solution?
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.
Or do you see a better solution for overriding
all document.write()'s?


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

Richard.
Jul 20 '05 #7

P: n/a
HikksNotAtHome wrote:
[...]
Is this comp.lang.javascript 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.
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?
PointedEars>
PointedEars>James Marshall wrote:
[aso.]


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

Jul 20 '05 #8

P: n/a
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bn******************@news.demon.co.uk...
<snip>
... , but window.open can be replaced with another function
directly (at least in all of the HTML browsers that I
can think of). ...

<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.
Jul 20 '05 #9

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...
[...]
Is this comp.lang.javascript 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.


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?
PointedEars>
PointedEars>James Marshall wrote:
[aso.]


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


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.
Jul 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.