slayeroffice - web experiments gone horribly awry

01.31.2006 - Functions...

Two new functions documented and archived today:

so_getElementsByAttributeValue
Originally a Tcl proc used to extend the functionality of the tdom XML parser binary for AOLserver, this function will return a collection of HTML objects whose attribute value matches attrValue. An optional second argument allows you to specify the attribute name to search. More useful in an XML DOM, but works in an HTML DOM as well, of course.
so_getText
Essentially a DOM based version of innerText -- pass it a handle to an object and it will pass back a string with the values of all the text nodes that descend from that object and that objects children.
First off... nice stuff as usual.

Second off... it doesn't seem to work in IE. I get several differnt errors depending on which version I use (passing the attribute to look in or not). With just a quick look, and based on PPK's findings on this page http://www.quirksmode.org/dom/w3c_core.html , it seems that IE doesn't support "hasAttribute" at all or "attributes" very well. Maybe I missed a warning about not working on IE, or am reading things wrong... but on two different machines running IE 6, we got no dice.

Third off... The original reason I was going to respond was to offer up an enhancement. Not sure how well the code will paste, so may have to have a follow up comment. But the jist of it is to change:

all[i].getAttribute(arguments[1]) == attrValue

to

(" "+all[i].getAttribute(arguments[1])+" ").indexOf(" "+attrValue+" ") != -1

That way if there is more then one class in the class attribute it can still match it. So if searching for "foo" it will match on "foo" and "foo bar", but not on "foobar".

Hopefully the above is helpful... now for the attempted full function paste attempt:

function so_getElementsByAttributeValue(attrValue,foo) {

var all = document.getElementsByTagName("*");

var elem = new Array()

var i=0;

while(all[i]) {

if(arguments[1]) {

if(all[i].hasAttribute(arguments[1])) if((" "+all[i].getAttribute(arguments[1])+" ").indexOf(" "+attrValue+" ") != -1) elem[elem.length] = all[i];

} else {

for(j=0;j<all[i].attributes.length;j++) if((" "+all[i].attributes[j].value+" ").indexOf(" "+attrValue+" ") != -1) elem[elem.length] = all[i];

}

i++;

}

return elem;

}
Posted by Aaron Barker on January 31, 2006 @ 9:58 pm


On my drive home from work it dawned on me that I had completely neglected to turn around to my windows machine and test these on IE. I feel awfully silly right now :)

Thanks for the post, Aaron - I'll do some testing and post some updates. I was thinking perhaps a "loose matching" param based on your suggestion that will allow for a strict match or a "contains" match.
Posted by Steve on February 1, 2006 @ 12:26 am


Ok, works in IE now. I borrowed heavily from my old getElementsByWhatever to handle the attribute checks...haven't decided yet on if I want to add the loose matching, though.
Posted by Steve on February 1, 2006 @ 4:04 pm
for so_getText(), if DOM3's property <a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent">textContent</a> exists, just return it. ;) I think FF and maybe Opera support it.
Posted by Steve Clay on February 4, 2006 @ 3:52 pm
Looks like its just FF that supports it, but that's still pretty cool -- thanks Steve. I've added it to the function.
Posted by Steve on February 4, 2006 @ 4:12 pm
Some code nitpicks:

Why do you use txt[txt.length] and not txt.push?

Why do you use while loop and not for loop?

is there a difference between "var txt = new Array()" and "var txt = []", other then the latter is a bit shorter?
Posted by splintor on February 7, 2006 @ 2:53 pm


>>Why do you use txt[txt.length] and not txt.push?

Old habbit - IE didnt support it until 5.5 as I recall.

>> Why do you use while loop and not for loop?

No real reason, just depends on my mood.

>> is there a difference between "var txt = new Array()" and "var txt = []",

Not to my knowledge, no. It's just the way I learned to do it.
Posted by Steve on February 7, 2006 @ 3:38 pm


Comments have been closed for this post.