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

document.getElementsByTagName emulator for Netscape 4?

P: n/a
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[i].id == name)
foundLayer = x[i];
else if (x[i].layers.length)
var tmp = getObjNN4(x[i],name);
if (tmp) foundLayer = tmp;
}
return foundLayer;
}
Jul 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a


2obvious wrote:
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

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

P: n/a
> 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,
<tables>, ... (Or maybe they're in a collection you just didn't happen
to mention, I don't know).
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.
Jul 20 '05 #3

P: n/a
"2obvious" <va********@hotmail.com> wrote in message
news:6e************************@posting.google.com ...
... . Really,
what I want to do is run each element through a for loop. <snip>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.
--

Example JavaScript DOM listings for: Opera 7.11,
Mozilla 1.2 and ICEbrowser 5.4
<URL: http://www.litotes.demon.co.uk/dom_root.html >
Jul 20 '05 #4

P: n/a
> 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.
Jul 20 '05 #5

P: n/a
va********@hotmail.com (2obvious) writes:
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
--
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 #6

P: n/a
"2obvious" <va********@hotmail.com> wrote in message
news:6e************************@posting.google.com ...
<snip>
... . 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).
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.