![]() |
|
|
|||||||
![]() |
|
|
Thread Tools |
Rating:
|
|
|
PM User | #1 |
|
The thread killer ![]() ![]() Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
![]() |
getElementsByAttribute
Could be rewritten to work in ie5w, ie5m, of course, but I like it like it is:
Code:
// document.getElementsByAttribute([string attributeName],[string attributeValue],[boolean isCommaHyphenOrSpaceSeparatedList:false])
document.getElementsByAttribute=function(attrN,attrV,multi){
attrV=attrV.replace(/\|/g,'\\|').replace(/\[/g,'\\[').replace(/\(/g,'\\(').replace(/\+/g,'\\+').replace(/\./g,'\\.').replace(/\*/g,'\\*').replace(/\?/g,'\\?').replace(/\//g,'\\/');
var
multi=typeof multi!='undefined'?
multi:
false,
cIterate=document.getElementsByTagName('*'),
aResponse=[],
attr,
re=new RegExp(multi?'\\b'+attrV+'\\b':'^'+attrV+'$'),
i=0,
elm;
while((elm=cIterate.item(i++))){
attr=elm.getAttributeNode(attrN);
if(attr &&
attr.specified &&
re.test(attr.value)
)
aResponse.push(elm);
}
return aResponse;
}
__________________
liorean <[lio@wg]> Articles: RegEx evolt wsabstract , Named Arguments Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards Last edited by liorean; 11-16-2005 at 06:37 PM.. |
|
|
|
|
|
PM User | #2 |
|
The thread killer ![]() ![]() Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
![]() |
OK, an ie5 compatible version. (untested)
Code:
// document.getElementsByAttribute([string attributeName],[string attributeValue],[boolean isCommaSeparatedList:false])
document.getElementsByAttribute=function(attrN,attrV,multi){
attrV=attrV.replace(/\|/g,'\\|').replace(/\[/g,'\\[').replace(/\(/g,'\\(').replace(/\+/g,'\\+').replace(/\./g,'\\.').replace(/\*/g,'\\*').replace(/\?/g,'\\?').replace(/\//g,'\\/');
var
multi=typeof multi!='undefined'?
multi:
false,
cIterate=typeof document.all!='undefined'?
document.all:
document.getElementsByTagName('*'),
aResponse=[],
re=new RegExp(multi?
'\\b'+attrV+'\\b':
'^'+attrV+'$'),
i=0,
elm;
while((elm=cIterate.item(i++))){
if(re.test(elm.getAttribute(attrN)||''))
aResponse[aResponse.length]=elm;
}
return aResponse;
}
__________________
liorean <[lio@wg]> Articles: RegEx evolt wsabstract , Named Arguments Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards Last edited by liorean; 09-24-2003 at 12:24 PM.. |
|
|
|
|
|
PM User | #3 |
|
New Coder ![]() Join Date: May 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
I've tested the script a bit, but it does not work out of the box with me (my fault?).
I had to put an if statement around this: if (attr){ [code] attr=elm.getAttributeNode(attrN); if(re.test(attr.value)) aResponse[aResponse.length]=elm; [code] }; Code:
aResponse=[];
length,
and then it's working fine in Mozilla and IE6. IE5 does not know getAttributeNode ( http://www.xs4all.nl/~ppk/js/?version5.html ), so maybe it is better to rewrite the script with getAttribute? It looks like one can use regexp's for attribute values? That's cool! I don't know exactly the function of multi. Is it used when you want only the attribute values when it is part of more than one value (e.g. attr="value1 value2")? I think it's a great script. The problems I have with it, are probably because I don't understand every aspect of it. |
|
|
|
|
|
PM User | #4 |
|
The thread killer ![]() ![]() Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
![]() |
There were points to using getAttributeNode over getAttribute, but I can't remember exactly what at the moment. (I wrote this in April, so I can't exactly recall the basis of that choice.)
The multi variable was originally intended for space separated lists such as the class attribute, but I found it could be used for comma separated lists and hyphen separated lists as well. Thus if false, this function matches the css [attrName=attrValue] syntax, while if true, it matches all of the the css syntaces [attrName~=attrValue], [attrName|=attrValue], [attrName*=attrValue]. Hmm, I'll rewrite the ie5 version. Does the advanced version work as it should? (can't remember how much testing I did on this before I posted it) [:Edit:] Both are rewritten a bit, since it seems I had a not-quite-working source as starting point. Do they work?
__________________
liorean <[lio@wg]> Articles: RegEx evolt wsabstract , Named Arguments Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards Last edited by liorean; 09-23-2003 at 11:08 PM.. |
|
|
|
|
|
PM User | #5 |
|
New Coder ![]() Join Date: May 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
The first version only works in IE6. In MozFirebird I get the error message: attr has no properties.
Version for IE5 now works fine in both IE6 and Mozilla. In IE5 I will test it tomorrow. As far as I can tell you can delete 'length,' in the list of variables. I'm going to bed now. |
|
|
|
|
|
PM User | #6 |
|
The thread killer ![]() ![]() Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
![]() |
Yep, length is an artefact from earlier versions. It's gone now. I've tried to fix the attr.value problem, don't know if I succeeded. (Too lazy to make a test page...)
__________________
liorean <[lio@wg]> Articles: RegEx evolt wsabstract , Named Arguments Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards |
|
|
|
|
|
PM User | #7 |
|
New Coder ![]() Join Date: May 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
The second one works fine in IE5 and Mozilla.
The fist one is not working. attr=elm.getAttributeNode(attrN); is returning null if there is no attribute specified. So attr.specified won't work. Making it this way: if(attr && re.test(attr.value)) and it works. |
|
|
|
|
|
PM User | #8 |
|
The thread killer ![]() ![]() Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
![]() |
In fact, I'll have to check for all three of them, just to avoid the possible case of a value of null, which would then match a possible attribute sought for, with the name of null.
__________________
liorean <[lio@wg]> Articles: RegEx evolt wsabstract , Named Arguments Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards |
|
|
|
|
|
PM User | #9 | |
|
New Coder ![]() Join Date: May 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Quote:
I'll do some more advanced testing tomorrow. |
|
|
|
|
|
|
PM User | #10 |
|
The thread killer ![]() ![]() Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
![]() |
Me paranoid? <mumble>... he must be one of them... must be cautious...</mumble> I guess I'm a bit to set at what-I-consider-the-right-approach sometimes (including taking all possible cases in consideration), but paranoid?
This discussion got me to remember something. This script and the testing of it is what prompted me to experiment and find this nice obscure Mozilla bug. Something hidden that deep in the implementation of something, you have to be paranoid to find... I prefer the word thorough to paranoid, though...
__________________
liorean <[lio@wg]> Articles: RegEx evolt wsabstract , Named Arguments Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards |
|
|
|
|
|
PM User | #11 |
|
New Coder ![]() Join Date: May 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Thorough!!! Yes, that was the word that I meant. Blame it on my bad English
I've tested it a bit further. It seems to work fine now in IE and Moz. [attrName~=attrValue] and [attrName|=attrValue] do work now, as I can understand. But [attrName*=attrValue] does not work. Should it be supposed to work with how the script is done now? re=new RegExp(multi?'\\b'+attrV+'\[\^\\b\]\*\\b':'^'+attrV+'$') is making it a bit better, but re=new RegExp(multi?'\\b\.*?'+attrV+'\[\^\\b\]\*\\b':'^'+attrV+'$') (getting a headache of these escaped regexps) would make it perfect, although this is not working in IE5. With many thanks to your regexp tutorial on evolt
|
|
|
|
|
|
PM User | #12 |
|
The thread killer ![]() ![]() Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
![]() |
Hmm, let me have a look at this... according to my testing (of just the regexes) it works in Moz, but I might be wrong.
__________________
liorean <[lio@wg]> Articles: RegEx evolt wsabstract , Named Arguments Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards |
|
|
|
|
|
PM User | #13 |
|
Moderator ![]() ![]() Join Date: May 2002
Location: San Jose, CA
Posts: 1,207
Thanks: 0
Thanked 8 Times in 7 Posts
![]() |
I wrote a little tutorial for George on DOM 2 TreeWalker, featuring a getElementsbyAttrMatch() method. It's a bit more advanced, and I'm waiting on clearance from George, but if you want a peek, let me know.
Oh, that's right, IE doesn't really support XML namespaces... darn...
__________________
"The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own." June 30, 2001 author, Abacus MathML Editor author, JavaScript Developer's Dictionary |
|
|
|
|
|
PM User | #14 |
|
The thread killer ![]() ![]() Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
![]() |
Heh, I thought of using either NodeIterator or TreeWalker, but gave them up in favour of iew support.
I believe Chris Nott has a version of this using just those on his site, however.
__________________
liorean <[lio@wg]> Articles: RegEx evolt wsabstract , Named Arguments Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|