473,231 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

how to extend document.getElementsBy*() in Firefox

Dormilich
8,658 Expert Mod 8TB
I want to add (prototype) a method to the node list returned by document.getElementsByName() (and related, i.e. NodeList resp. HTMLCollection).

unfortunately, neither the NodeList nor the HTMLCollection interface seem to be prototypable in Firefox. (NodeList works in Safari & Opera, don't know about IE)

has anyone of you an idea how to solve this?

note: this matter has already been discussed, though I didn't find any solution to this problem. (beside prototyping into Object interface).
Jun 16 '09 #1
11 4167
acoder
16,027 Expert Mod 8TB
I don't think this is possible. Either create a function which does the job or create your own object with the nodelist and then extend that instead.
Jun 16 '09 #2
Dormilich
8,658 Expert Mod 8TB
you mean substituting document.getElementsBy*() with my own function (kind of)?
Jun 16 '09 #3
gits
5,390 Expert Mod 4TB
may be you could live with something like that:

Expand|Select|Wrap|Line Numbers
  1. document.baseGetElementsByTagName = document.getElementsByTagName;
  2.  
  3. document.getElementsByTagName = function(n) {
  4.     top.console.log(this.baseGetElementsByTagName(n));
  5. }
  6.  
  7. document.getElementsByTagName('div');
kind regards
Jun 16 '09 #4
acoder
16,027 Expert Mod 8TB
@Dormilich
Not necessarily. You could have a function which does whatever you want to do with the node-list and pass it the result of document.getElementsBy*(). If that's not to your liking, create your own object and pass it the node-list and extend to your heart's content.
Jun 16 '09 #5
Dormilich
8,658 Expert Mod 8TB
@gits: that won't do, since I don't want to do that for every method that's returning a HTMLCollection.

@acoder: passing the NodeList as argument seems indeed the best way.

maybe I'll do both in a try-catch block…
Jun 17 '09 #6
gits
5,390 Expert Mod 4TB
hmmm ... what about that simple loop for that task? :) (FF + Firebug to test in console)

Expand|Select|Wrap|Line Numbers
  1. function extendMethod(fn) {
  2.     top.console.log(fn);
  3.  
  4.     document['base' + fn] = document[fn];
  5.  
  6.     document[fn] = function(n) {
  7.         nodeList = this['base' + fn](n);
  8.         nodeList = doSomethingWithNodeList(nodeList);
  9.  
  10.         return nodeList;
  11.     };
  12. }
  13.  
  14. for (var i in document) {
  15.     if (/getElements/.test(i)) {
  16.         extendMethod(i);
  17.     }
  18. }
  19.  
  20. function doSomethingWithNodeList(nl) {
  21.     top.console.log(nl);
  22. }
  23.  
  24. document.getElementsByClassName('normal');
  25.  
that would always pass the nodelist for all 'getElements'-methods to a function called 'doSomethingWithNodeList' ... before the 'getElements'-method returns

kind regards
Jun 17 '09 #7
Dormilich
8,658 Expert Mod 8TB
I have to bear that in mind, maybe I can use it sometimes…

that's what I want to do:
Expand|Select|Wrap|Line Numbers
  1. document.getElementsByClassName("attachEvent").addEventForEach("click", doSomething, false, some_param);
obviously the initial thought was to extend HTMLCollection or NodeList. but alas, I don't want to call a function (i.e. attach an event) every time I get a HTMLCollection from the document. seems like I have to use a function, where I pass the NodeList…

EDIT: hm, maybe I can do something with prototype's bind() method…
Jun 17 '09 #8
gits
5,390 Expert Mod 4TB
the construct does that exactly? here is just a litle adaption to have it a bit more generic :)

Expand|Select|Wrap|Line Numbers
  1. function extendMethod(fn) {
  2.     top.console.log(fn);
  3.  
  4.     document['base' + fn] = document[fn];
  5.  
  6.     document[fn] = function(n, eachFn) {
  7.         nodeList = this['base' + fn](n);
  8.         nodeList = eachFn(nodeList);
  9.  
  10.         return nodeList;
  11.     };
  12. }
  13.  
  14. for (var i in document) {
  15.     if (/getElements/.test(i)) {
  16.         extendMethod(i);
  17.     }
  18. }
  19.  
  20. document.getElementsByClassName('normal', function(n) {
  21.     for (var i in n) top.console.log(n[i]);
  22. });
  23.  
this expl. code could just simply integrated into any oo framework etc. and now it' s able to get an eachFn param passed to the getElements-method ... that is a function that should be called with every node of the returned nodelist.

kind regards
Jun 18 '09 #9
Dormilich
8,658 Expert Mod 8TB
@gits
in the end it's like calling eachFn(nodeList) inside the getElementsBy*() method. so I just moved the function call from outside to inside… well, for my current problem this rather seems not worth it.

thanks anyway for the insight into method manipulation.
Jun 18 '09 #10
gits
5,390 Expert Mod 4TB
:) nothing to thank for ... i currently think about such things too since you asked that question ... and for our current goal those ideas above might help us sometimes ;) ...

kind regards
Jun 18 '09 #11
Dormilich
8,658 Expert Mod 8TB
found some talk about DOM iteration 1, 2. this could be a useful starting point.
Jun 18 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: InvisibleMan | last post by:
Hi, Thanks for any help in advance... Okay, I have the JS listed below that calls for the display of the (DIV) tag... cookie function not included, as don't feel its necessary but you'll get the...
4
by: jacster | last post by:
Hi, I want to extend the functionality of my Firefox browser. Can you write mozilla plugins in Javascript; can anyone point me to some resources for doing this? If not, can I save Javascript...
2
by: Boobie | last post by:
I switched to using this function to create element: ---------------------------------------------------- function elem(name, attrs, style, text) { var e = document.createElement(name); if...
7
by: Matt Kruse | last post by:
Is it possible to extend the HTMLCollection prototype in Firefox (>=2.0)? It looks like I can do it, but it doesn't work: HTMLCollection.prototype.funk = function() { alert(this.length); }...
3
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: ...
13
by: RommelTJ | last post by:
Hi, My website (http://www.justiceinmexico.org/indextest.php) looks good in Firefox, but horrible in IE, and I think it's because of an error in the javascript of a free web ticker I got off the...
13
by: Gretsch | last post by:
Re XML, Javascript, IE, Firefox Hi, I have a very simple & small XML file (only 7 variables). The code works for IE, but not for Firefox. I've search around and found lots of alternatives, but...
5
by: SuneR | last post by:
Hi, I am having problems figuring out how to make Firefox behave, and output the HTML I want it to output. The thing I want done is actually quite simple. I have a <labeltag, and inside it, I...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.