![]() | |
#21
| |||
| |||
|
|
On Dec 9, 4:39 am, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de wrote: Peter Michaux wrote: Based on the repository design guidelines, the above could be substantially reduced to just [...] Notes: There is no need to check for the existence of "document" as no browser, NN4+ and IE4+, missing "document". There is no need to check that document.getElementById is a callable since there has never been a known implementation where document.getElementById that is not callable. If these are the guidelines for your project, to produce code that is not interoperable and inherently unreliable, I don't want to contribute. I somewhat agree with that sentiment, |
|
// Note that Array.prototype.reverse is not // tested as it is from JS 1.1 |
#22
| |||
| |||
|
|
On Dec 9, 4:32 am, Peter Michaux <petermich... (AT) gmail (DOT) com> wrote: Based on the repository design guidelines, the above could be substantially reduced to just if (document.getElementById) { var getEBI = function(id, d) { var el = (d||document).getElementById(id); if (el.id == id) { return el; } }; } That is an absolutely terrible way to implement a _runtime_ method. DOM interface calls are the most used in any script and they have the greatest impact to the performance. JS <=> DOM calls already much slower then JS <=> JS calls and cutting their performance even further for any bit is a crime to be punished :-) :-| Features/environment sniffing has to be done only _once_ on the instantiation stage |
#23
| |||
| |||
|
|
On Dec 9, 2:34 am, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote: On Dec 9, 4:39 am, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de wrote: Peter Michaux wrote: Based on the repository design guidelines, the above could be substantially reduced to just [...] Notes: There is no need to check for the existence of "document" as no browser, NN4+ and IE4+, missing "document". There is no need to check that document.getElementById is a callable since there has never been a known implementation where document.getElementById that is not callable. If these are the guidelines for your project, to produce code that is not interoperable and inherently unreliable, I don't want to contribute. I somewhat agree with that sentiment, I would say your codes says otherwise and that you agree that a line for feature testing should be drawn somewhere. |
|
[quoted from the original post in this thread] // Note that Array.prototype.reverse is not // tested as it is from JS 1.1 |
#24
| |||||||||
| |||||||||
|
|
On Dec 9, 12:02 am, Peter Michaux <petermich... (AT) gmail (DOT) com> wrote: |
|
I made a CSS selector function one time (something like jQuery has) and I found that any I have found that a combination of gEBI and gEBTN is all I ever need to home in on whatever elements I need to manipulate. |
|
// Another implementation for developers concerned with // the IE5 problem about doctype and comments if (document.getElementsByTagName && typeof getAnElement != 'undefined' && getAnElement().getElementsByTagName) { var getEBTN = function(tag, root) { var els = root.getElementsByTagName(tag); if (tag == '*' && !els.length && root.all) { var all = root.all; els = []; for (var i=0, ilen=all.length; i<ilen; i++) { var el = all[i]; I prefer not to declare variables inside of loops. And for long loops, a while that counts down to 0 will be faster. Granted, you have to reverse the results at the end. |
|
// The following conditional could be factored out // if necessary. if ((el.nodeType == 1 && el.tagName != '!') || (!el.nodeType && el.tagName)) { els[els.length] = el; } } } return els; }; } I haven't actually verified the IE5 bug yet and tested the above in a wide set of browsers. I'm just looking at the packaging of the concepts. It isn't actually a bug, but a limitation of IE5's implementation of gEBTN. It just doens't handle "*". |
|
Even though the two getEBTN wrappers use different fallbacks for IE4 it looks like they could be combined into one just like I've done here with no real performance penalty. If some browsers had document.all and not element.all then that would be a problem but has that ever been the case? That one isn't likely. It was the gEBTN inference that I wanted to avoid. The previous discussion about this stemmed from the fact that most libraries make similar inferences about addEventListener (which requires three implementations: window, document and element.) It was noted that agents do exist that support that method for elements, but not for window. |
|
The API should be considered here. A getEBTN wrapper function like the above could be called "getByCssSelector" and the calls to getEBTN would become a subset of the possible calls to "getByCssSelector". If you feel we must have a getByCssSelector, |
|
then that makes sense. I can't imagine a scenario where I would use such a thing. |
|
This is something I'd like to add to the repository eventually. I did a couple weeks of work on these types of functions a while ago and it is very handy for enlivening pages for unobtrusive JavaScript. (Cue "PointedEars" for a "lemmings" comment.) I haven't found a need for such queries and I agree with "PointedEars" |
|
in that event delegation is often the best way to go anyway. |
#25
| ||||
| ||||
|
|
On Dec 9, 12:02 am, Peter Michaux <petermich... (AT) gmail (DOT) com> wrote: [snip] I didn't make a time test but I think the simulation of Array.prototype.filter will be very slow. I made a CSS selector Considering that the use of the filter wrapper only helps IE5 and under, it does make sense to duplicate its functionality in the gEBTN wrapper. But that function and other JS1.6 array method wrappers are useful for other purposes where speed is less of an issue. I think that we should deal with those shortly. Looking at the rest of my "base" module, I think these topics should be considered next (in no particular order.) These extend what is currently being discussed: - Get child elements - Get first and last child element (with optionally specified tagName) - Get HTML and head elements These are needed to complete basic feature testing support: - Style support detection - createElement and XML parse mode detection - Element parent and owner document - Element "canHaveChildren" test Array and object testing, traversal and mutation: - isArray - push/pop - 1.6 Array method wrappers (filter, map, some, every, forEach, etc.) - For in filter. Events (needed to support more advanced feature testing): - addEventListener wrapper - DOMContentLoaded simulation Miscellaneous: - Get and set element text - Cookies (with optional support for ASP's keyed values.) - Plug-in detection I realize that the task of finalizing, adding and documenting each is a bottleneck, |
|
but perhaps we should start discussing some of these in parallel. |
|
Everybody has their own areas of interest, |
|
so this may increase overall participation in the project. |
#26
| |||
| |||
|
|
On Dec 9, 10:49 am, Peter Michaux <petermich... (AT) gmail (DOT) com> wrote: On Dec 9, 2:34 am, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote: On Dec 9, 4:39 am, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de wrote: Peter Michaux wrote: Based on the repository design guidelines, the above could be substantially reduced to just [...] Notes: There is no need to check for the existence of "document" as no browser, NN4+ and IE4+, missing "document". There is no need to check that document.getElementById is a callable since there has never been a known implementation where document.getElementById that is not callable. If these are the guidelines for your project, to produce code that is not interoperable and inherently unreliable, I don't want to contribute. I somewhat agree with that sentiment, I would say your codes says otherwise and that you agree that a line for feature testing should be drawn somewhere. I was talking about the issue he was referring to (gEBTN for document vs. element.) |
#27
| |||
| |||
|
|
On Dec 9, 1:14 am, "Evertjan." <exjxw.hannivo... (AT) interxnl (DOT) net> wrote: Peter Michaux wrote on 09 dec 2007 in comp.lang.javascript: // the simplest possible implementation if (document.getElementById) { var getEBI = function(id, d) { return (d||document).getElementById(id); }; } What is the use of testing for getElementById, as the js code will error anyway when getEBI() is subsequently called while not defined? The idea is to avoid errors. A function that calls getEBI should only test for the existance of gEBI and not document.getElementById. This is because the implementation of getEBI may be changed and may not use document.getElementById. |
#28
| |||
| |||
|
|
Peter Michaux wrote on 09 dec 2007 in comp.lang.javascript: On Dec 9, 1:14 am, "Evertjan." <exjxw.hannivo... (AT) interxnl (DOT) net> wrote: Peter Michaux wrote on 09 dec 2007 in comp.lang.javascript: // the simplest possible implementation if (document.getElementById) { var getEBI = function(id, d) { return (d||document).getElementById(id); }; } What is the use of testing for getElementById, as the js code will error anyway when getEBI() is subsequently called while not defined? The idea is to avoid errors. A function that calls getEBI should only test for the existance of gEBI and not document.getElementById. This is because the implementation of getEBI may be changed and may not use document.getElementById. Seems a bit stupid to me, Peter. |
|
If you plan to avoid any possible implementation change, better not programme at all. |
|
if (document.getElementById) { var getEBI = function(id, d) { return (d||document).getElementById(id); };} else { var getEBI = function(id, d) { return d.getElementById(id); }; }; |
#29
| ||||||||||||
| ||||||||||||
|
|
On Dec 9, 2:24 am, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote: On Dec 9, 12:02 am, Peter Michaux <petermich... (AT) gmail (DOT) com> wrote: [snip] I made a CSS selector function one time (something like jQuery has) and I found that any I have found that a combination of gEBI and gEBTN is all I ever need to home in on whatever elements I need to manipulate. I use something like a a getElementsByClassName sometimes but that otherwise that is my limit also. |
|
[snip] // Another implementation for developers concerned with // the IE5 problem about doctype and comments if (document.getElementsByTagName && typeof getAnElement != 'undefined' && getAnElement().getElementsByTagName) { var getEBTN = function(tag, root) { var els = root.getElementsByTagName(tag); if (tag == '*' && !els.length && root.all) { var all = root.all; els = []; for (var i=0, ilen=all.length; i<ilen; i++) { var el = all[i]; I prefer not to declare variables inside of loops. And for long loops, a while that counts down to 0 will be faster. Granted, you have to reverse the results at the end. I prefer countdown loops also but don't use them when order matters. |
|
Do you have a link to that previous discussion? If so please post it |
|
when we get to events. I didn't know such a case existed. |
|
The API should be considered here. A getEBTN wrapper function like the above could be called "getByCssSelector" and the calls to getEBTN would become a subset of the possible calls to "getByCssSelector". If you feel we must have a getByCssSelector, I don't think it is a must. |
|
then that makes sense. I can't imagine a scenario where I would use such a thing. I can only imagine myself using one that takes simple selectors like "div", "#fooId", ".fooClass" and combinations of those three. A more complex selector is only needed if the content creator is totally uncooperative and won't add the hooks needed by the JavaScript programmer. |
|
The programming of a getByCssSelector sure is fun. Probably the most |
|
enjoyable thing I've programmed in JavaScript that might be useful as it involves a CSS selector string parser and optimization is a major priority. |
|
[snip] This is something I'd like to add to the repository eventually. I did a couple weeks of work on these types of functions a while ago and it is very handy for enlivening pages for unobtrusive JavaScript. (Cue "PointedEars" for a "lemmings" comment.) I haven't found a need for such queries and I agree with "PointedEars" That is the fourth and a half time that has happened recently. |
|
in that event delegation is often the best way to go anyway. I don't see them as mutually exclusive as there are only so many |
|
elements on which to delegate and listeners concerned with different handlers should not be combined. I really don't get what he is talking |
|
about, he has told me I don't understand, and he is write. |
#30
| ||||||||
| ||||||||
|
|
On Dec 9, 1:39 am, Thomas 'PointedEars' Lahn <PointedE... (AT) web (DOT) de> wrote: Peter Michaux wrote: Based on the repository design guidelines, the above could be substantially reduced to just [...] Notes: There is no need to check for the existence of "document" as no browser, NN4+ and IE4+, missing "document". There is no need to check that document.getElementById is a callable since there has never been a known implementation where document.getElementById that is not callable. If these are the guidelines for your project, to produce code that is not interoperable and inherently unreliable, Do you feature test everything? |
|
Do you feature test that alert is callable? |
|
Do you feature test that string concatenation works? |
|
Do you feature test that float division works (e.g. 1/2 is 0.5)? |
|
Do you feature test for the bugs in the implementation of feature "x" in browser "y" that you have never seen? |
|
You could almost do this by an insanely laborious process of checking things like float division in 100 different combinations before doing float division. |
|
Hopefully you answer "no" to at least one of those question. If so then you see that |
|
the line has to be drawn somewhere. |
![]() |
| Thread Tools | |
| Display Modes | |
| |