Closed Thread Icon

Topic awaiting preservation: Newbie && DOM Anomalie Question (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8874" title="Pages that link to Topic awaiting preservation: Newbie &amp;amp;&amp;amp; DOM Anomalie Question (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Newbie &amp;&amp; DOM Anomalie Question <span class="small">(Page 1 of 1)</span>\

 
Dick Ulrich
Bipolar (III) Inmate

From: Dublin, TX, USA
Insane since: Sep 2003

posted posted 09-28-2003 07:43

Hello, this is my first time here, so excuse my shyness.
This question involves two different (but similar functions) I call FEO and FEOS.

I have written a 'ForEachObject' function (FEO) whose purpose is to traverse all objects on an HTML page.
The result of the function is usually an array of object references. However, given a function to apply (fa) the result can be an array of whatever is of interest (such as the names of classes, etc.). If the function to apply
returns 'null' for all items, then the result will be an empty array. This makes it useful as a filter that can also
alter the objects being traversed.

I wish to use the function to Query as well as Alter objects.

The code for FEO:
//arg - fa - function to be applied (or null) is invoked by 'FEO'
// Note: 'fa' is given each element reference by 'FEO'
// Note: If 'fa' returns null, no result element is appended.
//arg - anpresent - attribute name (or null)
// Note: Sense is - If object has this 'attributename', continue
//arg - anabsent - attribute name (or null)
// Note: Sense is - If object does not have this 'attributename', continue

function FEO(fa,anpresent,anabsent)
{
var res = new Array();
var els = document.getElementsByTagName("*");
for (var elsidx=residx=0; elsidx < els.length; elsidx++)
{
var el = els[elsidx];
if ((typeof el) == 'object')
{
var ares = el; //an assumption
if (anpresent && !el[anpresent]) { continue; }
if (anabsent && el[anabsent]) { continue; }
if (fa) { ares = fa(el); }
if (ares) res[residx++] = ares;
}
}
return res;
}

Scenario: Return a list of all objects.

objs = FEO();

Scenario: Return a list of all objects if they contain the 'className' attribute.

objs = FEO(null, 'className');

Scenario: Return a list of all objects if they do not contain the 'className' attribute.

objs = FEO(null, null, 'className');

Scenario: I wish to change an object class from 'firstclass' to 'secondclass'.

function ChangeClass(el)
{
if (el.className == 'firstclass') { el.className = 'secondclass'; }
return null;
}
FEO(ChangeClass, 'className');

Scenario: I wish to set an object class to 'noclass' if it has no class.

function SetNoClass(el)
{
el.className = 'noclass';
}
FEO(SetNoClass, null, 'className');

COMMENT: This function seems to work (given my level of understanding). However, a similar one I call FEOS (ForEachObjectStyle) seems to not appreciate me (IE 6+). Some style attributes such as 'position', 'width', etc.
the function can detect, but others it doesn't. I am a little green yet, so I am having difficulty discerning the reality of it all.

function FEOS(fa,anpresent,anabsent)
{
var res = new Array();
var els = document.getElementsByTagName("*");
for (var elsidx=residx=0; elsidx < els.length; elsidx++)
{
var el = els[elsidx];
if (((typeof el) == 'object') && el.style)
{
var ares = el; //an assumption
if (anpresent && !el.style[anpresent]) { continue; }
if (anabsent && el.style[anabsent]) { continue; }
if (fa) { ares = fa(el); }
if (ares) res[residx++] = ares;
}
}
return res;
}

Sample for FEOS:

var objs = FEOS(null,'position');
for (var i=0; i < objs.length; i++)
{
obj = objs[i];
alert(obj.id + ' osition = ' + obj.style.position);
}

FOR YOU:
1. Can you give me ideas on improving this code?
2. Can you give me ideas on how to make it cross-browser?
2. I only have IE here, will this code work on other browsers?

For a first timer, I have taken up a lot of your time.
I do appreciate any info you can/might shed.
I will reciprocate as I learn for other newbies.

quisja
Paranoid (IV) Inmate

From: everywhere
Insane since: Jun 2002

posted posted 09-28-2003 13:16

If you want people testing it cross-browser then you should really try to have a sample page online. Whatever you're developing for the web you really shouldn't be testing it in I.E. only. You want to look at downloading Firebird and Opera as a bare minimum. Saying that, there's nothing really obvious in the code of your first function which would make it fail in those browsers.

Dick Ulrich
Bipolar (III) Inmate

From: Dublin, TX, USA
Insane since: Sep 2003

posted posted 09-28-2003 13:29

I agree with you, I will try to get other browsers soon.
I am thinking that I must not be seeing all objects?
I thought that document.getElementsByTagName("*") would yield all of the object tree.
Yet some reading I was just doing indicates that this may not be the case.
Examples seem to be recursive in order to walk the branches of the tree.
Is there not a way to visit all objects without the recursion?

Thanks ...

« BackwardsOnwards »

Show Forum Drop Down Menu