|
|||||||
New to SitePoint Forums? Register here for free!
|
| View Poll Results: What are the best names for these methods? | |||
| get(), getAll() |
|
1 | 12.50% |
| match(), matchAll() |
|
1 | 12.50% |
| getElementBySelector(), getElementsBySelector() |
|
6 | 75.00% |
| getElementBySelector(), getElementListBySelector() |
|
0 | 0% |
| Voters: 8. You may not vote on this poll | |||
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
⠵
![]() Join Date: Dec 2004
Location: Sweden
Posts: 2,512
|
Influence a future W3C Standard: Selectors API
Hey,
There has been a lot of discussion on the public-webapi@w3.org mailing list about what names these methods should have. I was interested in what you guys think are the best names. What do they do? Simply put, you can get either the first element in the document, or a static nodelist of all elements in the document, that match a group of selectors, using two different methods. They can both be used on Documents and on Elements. Example (using "XXX" and "YYY" as method names here, respectively): Code:
var e = document.XXX("#nav ul");
var items = e.YYY("li"); // static nodelist
See the working draft of the spec, or the editor's draft. So what do you think are appropriate names for these two methods? Why?
__________________
Simon Pieters (works for Opera Software, though represents just himself) |
|
|
|
|
|
#2 |
|
⠵
![]() Join Date: Dec 2004
Location: Sweden
Posts: 2,512
|
Oops. The last two should have been:
__________________
Simon Pieters (works for Opera Software, though represents just himself) |
|
|
|
|
|
#3 |
|
CSS & JS/DOM Adept
![]() Join Date: Mar 2005
Location: USA
Posts: 5,486
|
Why would there be two different methods? Why would you want a method that could only ever return a single element?
__________________
We miss you, Dan Schulz. Learn CSS. | X/HTML Validator | CSS validator Dynamic Site Solutions Code for Firefox/Safari/Opera, add fixes for IE, not vice versa. |
|
|
|
|
|
#4 |
|
SitePoint Author
![]() ![]() ![]() Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 12,249
|
For efficiency reasons, probably. A simple ID selector can never match more than one element, so returning an array or node list is unnecessary.
Returning a single element node would let us skip the array index reference. Compare to these existing functions: Code:
var e1 = document.getElementById("foo");
var e2 = document.getElementsByTagName("body").item(0);
|
|
|
|
|
|
#5 |
|
⠵
![]() Join Date: Dec 2004
Location: Sweden
Posts: 2,512
|
Yeah, it's for performance. The second method will have to walk the entire tree, and the common case is probably that you just want one element matching the selector.
__________________
Simon Pieters (works for Opera Software, though represents just himself) |
|
|
|
|
|
#6 |
|
SitePoint Author
![]() ![]() ![]() Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 12,249
|
|
|
|
|
|
|
#7 |
|
SitePoint Member
Join Date: Aug 2003
Location: Netherlands
Posts: 13
|
Well, personally I prefer getElementsBySelector() instead of getElementListBySelectors(), but I think it is at least a lot better than getAll(), which I don't like at all.
I think the getElementBySelectors() method is totally unnecessary. |
|
|
|
|
|
#8 | |
|
⠵
![]() Join Date: Dec 2004
Location: Sweden
Posts: 2,512
|
Quote:
Off Topic: Climbing sounds more like an all-out effort, while walking the tree is actually a pretty simple task. ![]() Well except the trees that come from IE, you can get around in circles in those and see the same things twice! More like a bush full of weeds and fungus.
__________________
Simon Pieters (works for Opera Software, though represents just himself) |
|
|
|
|
|
|
#9 |
|
I'll take mine raw
![]() Join Date: Dec 2002
Location: Alabama, USA
Posts: 2,562
|
getElementsBySelectors() is more in line with the existing naming convention - even tho matchOne/matchAll is certainly easier to type. But having only one letter difference between two (long) function names is just asking for typo bugs
![]() Seeing as how IE only just now supports CSS1 - I think we'll be using our own Js implementations of these functions for a long time to come ![]() |
|
|
|
|
|
#10 | |
|
CSS & JS/DOM Adept
![]() Join Date: Mar 2005
Location: USA
Posts: 5,486
|
Quote:
Even if an ID selector was used at the beginning of the selector chain?
__________________
We miss you, Dan Schulz. Learn CSS. | X/HTML Validator | CSS validator Dynamic Site Solutions Code for Firefox/Safari/Opera, add fixes for IE, not vice versa. |
|
|
|
|
|
|
#11 |
|
⠵
![]() Join Date: Dec 2004
Location: Sweden
Posts: 2,512
|
Yeah. Multiple elements can have the same ID. It is invalid, mind you, but it would "work" just like it already does in CSS.
__________________
Simon Pieters (works for Opera Software, though represents just himself) |
|
|
|
|
|
#12 |
|
Islomaniac
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Cambridge, UK
Posts: 4,218
|
This is great, but when you say browsers will implement it, has Microsoft made any noise about when they're going to update their javascript engine?
getElementListBySelector() is a bit of a mouthful and get()/getAll() sounds Microsoftish (like document.all) so I agree with the others that it's best to stick with the name convention - I vote for option 3. |
|
|
|
|
|
#13 |
|
CSS & JS/DOM Adept
![]() Join Date: Mar 2005
Location: USA
Posts: 5,486
|
Perhaps getOneElementBySelectors() and getElementsBySelectors() would be less prone to unnoticed typos.
__________________
We miss you, Dan Schulz. Learn CSS. | X/HTML Validator | CSS validator Dynamic Site Solutions Code for Firefox/Safari/Opera, add fixes for IE, not vice versa. |
|
|
|
|
|
#14 |
|
Islomaniac
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Cambridge, UK
Posts: 4,218
|
What about merging both into one: getElement(s)BySelector(s)()?
On a more serious note, am I right in thinking that getElementBySelectors('#nav li.even') would only match the first li.even in the ul? Or would it be the last? |
|
|
|
|
|
#15 | ||
|
⠵
![]() Join Date: Dec 2004
Location: Sweden
Posts: 2,512
|
Quote:
Quote:
__________________
Simon Pieters (works for Opera Software, though represents just himself) |
||
|
|
|
|
|
#16 |
|
Programming Since 1978
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 12,867
|
Since having a second element with the same ID is invalid there is no need for a browser to continue looking once it finds the first one since the subsequent ones shouldn't be there and it is therefore more correct to ignore them and assume that they don't exist than it would be to continue searching for something which in a valid page wont be there. Any browser that continues searching once it finds the first occurrence of an id is just being inefficient.
__________________
Stephen J Chapman HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog Book Reviews, About JavaScript scripts and tutorials follow me on Twitter |
|
|
|
|
|
#17 | |
|
Programming Since 1978
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 12,867
|
Quote:
__________________
Stephen J Chapman HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog Book Reviews, About JavaScript scripts and tutorials follow me on Twitter |
|
|
|
|
|
|
#18 | |
|
⠵
![]() Join Date: Dec 2004
Location: Sweden
Posts: 2,512
|
Quote:
However, I've asked the WG about this case. If it ends up making the spec clearer then that's a good thing.
__________________
Simon Pieters (works for Opera Software, though represents just himself) |
|
|
|
|
|
|
#19 |
|
Programming Since 1978
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 12,867
|
Presumably after determining that there are multiple elements with the same id on a web page the browser will then report an ERROR and refuse to process any of them since the page is invalid and to do anything else would be compounding the error. Only by IGNORING any subsequent occurrences and treating the page as if only the first one exists is any consistent approach that doesn't involve giving an error possible. So presumably if the search for subsequent occurrences is required to continue past the first occurrence of an ID the browser will be required to abort the display of the page as soon as an invalid second occurrence is found. At least aborting the display of the page will make it more obvious to the author of the page that they have made a stupid error.
__________________
Stephen J Chapman HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog Book Reviews, About JavaScript scripts and tutorials follow me on Twitter |
|
|
|
|
|
#20 | |
|
I'll take mine raw
![]() Join Date: Dec 2002
Location: Alabama, USA
Posts: 2,562
|
Quote:
![]() |
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 05:31.

















Linear Mode
