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.