473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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.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
9 6304
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
"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
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
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
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
"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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Eli | last post by:
I've got a script that I'm trying to debug which uses document.write() to place HTML within a page. In both IE6 and Firefox when I view source, I see only the script itself and not any HTML as...
2
by: John Mack | last post by:
Intermittently I get the following error on Firefox: "Error: uncaught exception: Permission denied to get property HTMLDocument.window" What can cause this error? I do an image switch via JS...
0
by: Filippo Bettinaglio | last post by:
VS2005, C# I have developed a UserControl embedded in a HTML web page. And I can access to the DOM with the following code: HTML page: …….. <BODY onload=loadDoc()> …….
2
by: Paul Hemans | last post by:
I am very new at .Net. I have a small project where I need to manipulate the contents of a web page. I have a form with a web browser control (webBrowser1) on it. Within the...
9
by: Le Minh | last post by:
Hi, i want to write a program. Input of this is HTML source code of a web page and output is a treeview representation it structure. I want to write it with HtmlDocument in .net framework 2.0. how...
5
by: Jeff | last post by:
Is there a standard way of getting the HTMLDocument object representation of a remote page using Javascript? If I request an HTML page, the xmlHttpRequest returns either text or an XMLDocument. I...
0
by: nickin4u | last post by:
I have a application that is used to automate certain task, I have been using mshtml.HTMLDocument class but certain events like click a button do not fire. I have tried a number of combinations but...
2
by: CSharper | last post by:
Is there a class I can use which loads HtmlDocument and performs default html validation to see if the document is a valid html document like XDocument? HtmlDocument seems to me only used to create...
1
by: orensol | last post by:
Hello, I am trying to override document.write, because there are cases in which a slow response from a <script src=...request which produces document.write calls is received after the page has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.