document.getElementsByTagName emulator for Netscape 4?

2

2obvious

This is a pipe dream, I realize, but I'm trying to emulate the
functionality of the W3C DOM-supported document.getElementsByTagName
method under the very nightmarish Netscape 4.

Through some sleuthing, I was able to find what serves as a
document.getElementById emulator at
http://www.xs4all.nl/~ppk/js/dhtmloptions.html#versionb.

(Below is the code; this clever algorithm is painstakingly explained
at the site above.)

Anyhoo: I figured that by zeroing in on the tag property (if such a
property exists) instead of the ID property, I would be well on my way
to emulating getElementsByTagName. But I've had no success, and I
can't find a comprehensive enough explanation of Netscape 4's layered
DOM to figure out how to zoom in on properties like className or tag.

If anyone knows how to get a tag under NN4, or knows of a thorough NN4
DOM explication online, or knows how to hack this code to make it
emulate get...tag, I would greatly appreciate your insight.


function getObj(name)
{
if (document.getElementById)
{
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
}
else if (document.all)
{
this.obj = document.all[name];
this.style = document.all[name].style;
}
else if (document.layers)
{
this.obj = getObjNN4(document,name);
this.style = this.obj;
}
}

function getObjNN4(obj,name)
{
var x = obj.layers;
var foundLayer;
for (var i=0;i<x.length;i++)
{
if (x.id == name)
foundLayer = x;
else if (x.layers.length)
var tmp = getObjNN4(x,name);
if (tmp) foundLayer = tmp;
}
return foundLayer;
}
 
M

Martin Honnen

2obvious said:
This is a pipe dream, I realize, but I'm trying to emulate the
functionality of the W3C DOM-supported document.getElementsByTagName
method under the very nightmarish Netscape 4.

Through some sleuthing, I was able to find what serves as a
document.getElementById emulator at
http://www.xs4all.nl/~ppk/js/dhtmloptions.html#versionb.

(Below is the code; this clever algorithm is painstakingly explained
at the site above.)

Anyhoo: I figured that by zeroing in on the tag property (if such a
property exists) instead of the ID property, I would be well on my way
to emulating getElementsByTagName. But I've had no success, and I
can't find a comprehensive enough explanation of Netscape 4's layered
DOM to figure out how to zoom in on properties like className or tag.

If anyone knows how to get a tag under NN4, or knows of a thorough NN4
DOM explication online, or knows how to hack this code to make it
emulate get...tag, I would greatly appreciate your insight.

Netscape 4 provides collections like
document.links
document.images
document.forms
document.embeds
document.applets
And inside a <form> control you can access the <input> and <textarea>
elements as
document.forms.formName.elements
That is the only way to access elements by tag name thus the only
functionality for an NN4 document.getElementsByTagName would be
document.getElementsByTagName = function (tagName) {
switch (tagName.toLowerCase()) {
case 'img':
return document.images;
case 'applet':
return document.applets;
...
but I frankly see no advantage in that approach as those collections are
provided by IE and other browsers as well so you can directly script
them cross browser without any need for getElementsByTagName
 
2

2obvious

Netscape 4 provides collections like
document.links
document.images
document.forms
document.embeds
document.applets
And inside a <form> control you can access the <input> and <textarea>
elements as
document.forms.formName.elements
That is the only way to access elements by tag name thus the only
functionality for an NN4 document.getElementsByTagName would be
document.getElementsByTagName = function (tagName) {
switch (tagName.toLowerCase()) {
case 'img':
return document.images;
case 'applet':
return document.applets;
...

I think I follow your explanation. However, immediately I notice a
few tags that don't fall under these collections ): <div>s, <span>s,
... (Or maybe they're in a collection you just didn't happen said:
but I frankly see no advantage in that approach as those collections are
provided by IE and other browsers as well so you can directly script
them cross browser without any need for getElementsByTagName

Well, shoot, now that I see how NN4 looks at things, I'm inclined to
agree with you. The deal is: I'm not searching for one specific tag,
but _all_ tags:

document.getElementsByTagName("*")

I do this to get an array of all the elements in a page. Really, what
I want to do is run each element through a for loop.

So now I guess my question changes (and maybe I should repost it under
a clearer name?).

New question: is there any way to look at each element individually
under Netscape 4? Perhaps (as in this case) there's a way of doing
this that will work cross browser.
 
R

Richard Cornford

2obvious said:
... . Really,
what I want to do is run each element through a for loop.
New question: is there any way to look at each element
individually under Netscape 4? Perhaps (as in this case)
there's a way of doing this that will work cross browser.

The Netscape 4 DOM just does not expose the majority of elements for
scripting. Martin has already explained how to get at what can be got at
on Netscape 4.

So my question is; why do you want to run each element through a loop?
Knowing that there may be an alternative approach that would get you
what you need, or someone could say with certainty that it cannot be
done.

Richard.
 
2

2obvious

The Netscape 4 DOM just does not expose the majority of elements for
scripting. Martin has already explained how to get at what can be got at
on Netscape 4.

(Man, I am so useless without a reference!)

I started this thread during Independence Day weekend, my copy of
_Javascript: The Definitive Guide_ sitting at work. Not that I doubt
Martin, but it's easier to accept statements when there's a
comprehensive explanation that follows. (The difference between
knowing and understanding? I hope you understand.)
So my question is; why do you want to run each element through a loop?
Knowing that there may be an alternative approach that would get you
what you need, or someone could say with certainty that it cannot be
done.

Well, when I initially thought of the question, there was an algorithm
in mind. But even before posting the question, I thought of some
other way to address this problem. So my concern was theoretical.

I learned javascript working with the W3C DOM standard, and
document.getElementById() and document.getElementsByTagName() are by
far my most favored functions. Work is now forcing me to embrace
Netscape 4. Theoretically, if I could emulate these functions under
NN4, I could build a small, cross-browser DOM of my own, and spare
myself the hassle of having to rewrite a bunch of logic.

Well, getElementById() can be emulated. getElementsByTagName(), not
so much. I have my answer. Thanks for your consideration group,
especially Martin and Richard!

--E.
 
L

Lasse Reichstein Nielsen

Well, getElementById() can be emulated.

Not really. Try adding
<em id="dims">thingie</em>
and see if you can find a way of accessing that em-element.

Netscape 4 doesn't allow Javascript access to all nodes. It has a
number of collections (images, forms, links, anchors, embeds, applets,
and the one that is usually the necessary one: layers, for the
absolutely positioned elements), but they don't range over all
possible elements.

Likewise, NS4 don't allow you to traverse the DOM tree yourself,
missing the root (document.body) and the children references.

NS4 sucks, big time, and if you have to support it, you have my
sympathies.

/L
 
R

Richard Cornford

... . Not that I doubt Martin, but it's easier to accept
statements when there's a comprehensive explanation that
follows. (The difference between knowing and understanding?
I hope you understand.)

I understand entirely, knowing why something is the case is always
better than just being told by some authority that it is the case
(especially given the number of times that understanding the mechanism
behind some assertion has exposed/suggested and exception that can be
exploited). Unfortunately time constraints make writing everyone a
detailed explanation for every question impractical (and repetitive).
I learned javascript working with the W3C DOM standard, and
document.getElementById() and document.getElementsByTagName()
are by far my most favored functions. Work is now forcing me
to embrace Netscape 4. Theoretically, if I could emulate these
functions under NN4, I could build a small, cross-browser DOM
of my own, and spare myself the hassle of having to rewrite a
bunch of logic.
<snip>

I am getting the impression that being encouraged to write W3C DOM
standard code (which is completely understandable in itself) is leading
to people fixating on DOM Core code at the expense of the HTML DOM Level
2 standard. Examining the HTML DOM Level 2 standard documentation at the
W3C shows that an HTMLDocument element has various specified properties
including a number of HTMLCollection properties; anchors, applets,
forms, images and links. None of which are deprecated and only the
images collection is subject to additional comment.

That means that, with the exception of document.embeds (as embeds are
not in HTML 4) and document.layers, the collections provided by Netscape
4 are still part of the DOM standard and can be accessed with code that
conforms to that standard.

So, for example, code that is written to use getElementById to access a
form or its contents because it is "DOM Standard" is misguided as it
will predictably fail where code that accesses a form via the
document.forms collection is in reality in _equal_ compliance [1] with
the published standards but will often also provide considerably greater
compatibility with ancient and unusual browsers.

Whiting cross-browser code that will include Netscape 4 would
concentrate on doing what can be done via Netscape 4's collection. If
something can't be done via those collections then the chances are that
Netscape 4 just can't do it at all. But the bulk of the resulting code
can still be HTML DOM Level 2 standard code and will also work on all
standards compliant browsers (along with many others). (The Netscape 4
proprietary document.layers collection being an obvious exception).

Richard.

[1] Jim Ley recently (and correctly) pointed out that the - document -
property of the global object does not feature in any W3C (or ECMA)
standard. So while - getElementById - and - forms - may both be
specified in the DOM standards prefixing either with - document. - is
relying on an unspecified browser implementation detail. Of course with
no DOM specified mechanism for accessing the document element from the
global object it is in reality impossible to write truly W3C DOM
compliant code with JavaScript (though in practice that is more a source
of humour than an imposition on script authoring).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top