473,815 Members | 3,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extend HTMLCollection. prototype with Firefox?

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.leng th); }
window.onload= function() {
var x = document.getEle mentsByTagName( 'div');
alert(HTMLColle ction.prototype .funk); // function() { ... }
alert(x instanceof HTMLCollection) ; // true
alert(x.item=== HTMLCollection. prototype.item) ; // false
x.funk(); // error: x.funk is not a function
}

Is there any other way to do it that will work right now?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 2 '07 #1
7 4695
On Feb 2, 4:58 am, Matt Kruse wrote:
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.leng th); }
window.onload= function() {
var x = document.getEle mentsByTagName( 'div');
alert(HTMLColle ction.prototype .funk); // function() { ... }
alert(x instanceof HTMLCollection) ; // true
alert(x.item=== HTMLCollection. prototype.item) ; // false
x.funk(); // error: x.funk is not a function

}

Is there any other way to do it that will work right now?
The - getElementsByTa gName - method is specified as returning a
NodeList, but may in reality return a NamedNodeMap. If you want to
draw conclusions about objects implementing HTMLCollection shouldn't
you use a source that is specified as being one, such as -
document.forms -?

Richard.

Feb 2 '07 #2

Richard Cornford wrote:
On Feb 2, 4:58 am, Matt Kruse wrote:
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.leng th); }
window.onload= function() {
var x = document.getEle mentsByTagName( 'div');
alert(HTMLColle ction.prototype .funk); // function() { ... }
alert(x instanceof HTMLCollection) ; // true
alert(x.item=== HTMLCollection. prototype.item) ; // false
x.funk(); // error: x.funk is not a function

}

Is there any other way to do it that will work right now?

The - getElementsByTa gName - method is specified as returning a
NodeList, but may in reality return a NamedNodeMap.
Doesn't the line:

alert(x instanceof HTMLCollection) ; // true

indicate that it is an HTMLCollection?
--
Rob

Feb 2 '07 #3
Richard Cornford wrote:
The - getElementsByTa gName - method is specified as returning a
NodeList, but may in reality return a NamedNodeMap.
I don't know much in this area, but I thought NodeList was the interface,
and that HTMLCollection was the implementation. I tried to extend NodeList
also, but I got the same results. Also, since (x instanceof HTMLCollection)
== true, I thought I was on the right track. If it doesn't return an
HTMLCollection object, why would that check be true?
If you want to
draw conclusions about objects implementing HTMLCollection shouldn't
you use a source that is specified as being one, such as -
document.forms -?
I just tried that as well with the same results.

Nevertheless, do you know which object, if any (except Object), I could
extend to add a method to the results returned by getElementById( )?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 2 '07 #4
Matt Kruse wrote:
Richard Cornford wrote:
>The - getElementsByTa gName - method is specified as returning a
NodeList, but may in reality return a NamedNodeMap.

I don't know much in this area, but I thought NodeList was the
interface, and that HTMLCollection was the implementation.
HTMLCollection is an interface, just line NodeList.
I tried to extend NodeList also, but I got the same results.
Also, since (x instanceof HTMLCollection) == true, I thought
I was on the right track. If it doesn't return an HTMLCollection
object, why would that check be true?
The - instanceof - operator is not necessarily usefully discriminating
in javascript.
>If you want to draw conclusions about objects implementing
HTMLCollecti on shouldn't you use a source that is specified
as being one, such as - document.forms -?

I just tried that
Good, as starting with something that is supposed to be an -
HTMLCollection - eliminates possible sources of interference.
as well with the same results.
Remember that - NodeList - and - NamedNodeMap - are specified (in the
ECMAScript bindings) as mapping bracket notation (and therefore, due to
ECMA 262 provisions, dot notation) property accessors onto their - item -
methods, and - HTMLCollection - is specified as mapping them onto its -
item - and - namedItem - methods. So your - x.funk - may be equivalent
to - x.namedItem('fu nc') -, and as - namedItem - may only return an
object implementing the - Node - interface or null it should not be too
surprising if you don't get access to your method.
Nevertheless, do you know which object, if any (except Object),
I could extend to add a method to the results returned by
getElementById( )?
Objects implementing the - Element - interface are returned by-
getElementById -.

Richard.

Feb 3 '07 #5
RobG wrote:
Richard Cornford wrote:
>>On Feb 2, 4:58 am, Matt Kruse wrote:
>>Is it possible to extend the HTMLCollection prototype in
Firefox (>=2.0)? It looks like I can do it, but it doesn't
work:
<snip>
>>window.onload = function() {
var x = document.getEle mentsByTagName( 'div');
<snip>
>The - getElementsByTa gName - method is specified as returning a
NodeList, but may in reality return a NamedNodeMap.

Doesn't the line:

alert(x instanceof HTMLCollection) ; // true

indicate that it is an HTMLCollection?
It indicates that - HTMLCollection. prototype - is on the prototype chain
of - x -, assuming that as a host object - HTMLCollection - does not take
advantage of its option to provide a non-standard - [[HasInstance]] -
method. That does not even imply that the object - x - implements the -
HTMLCollection - interface.

Richard.

Feb 3 '07 #6
Richard Cornford wrote:
Remember that - NodeList - and - NamedNodeMap - are specified (in the
ECMAScript bindings) as mapping bracket notation (and therefore, due
to ECMA 262 provisions, dot notation) property accessors onto their -
item - methods, and - HTMLCollection - is specified as mapping them
onto its - item - and - namedItem - methods. So your - x.funk - may
be equivalent to - x.namedItem('fu nc') -, and as - namedItem - may
only return an object implementing the - Node - interface or null it
should not be too surprising if you don't get access to your method.
That makes sense, I hadn't even considered that.
>Nevertheless , do you know which object, if any (except Object),
I could extend to add a method to the results returned by
getElementById ()?
Objects implementing the - Element - interface are returned by-
getElementById -.
Oops, what I meant was getElementsByTa gName() of course. Do you know of any
way to add methods to the prototype of the Object returned by this call in
Firefox? (Other than Object.prototyp e).

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 3 '07 #7
Matt Kruse wrote:
Richard Cornford wrote:
<snip>
>... . So your - x.funk - may be equivalent to - x.namedItem('fu nc')
-, and as - namedItem - may only return an object implementing the
- Node - interface or null it should not be too surprising if you
don't get access to your method.

That makes sense, I hadn't even considered that.
>>Nevertheles s, do you know which object, if any (except Object),
I could extend to add a method to the results returned by
getElementByI d()?
Objects implementing the - Element - interface are returned by-
getElementBy Id -.

Oops, what I meant was getElementsByTa gName() of course.
Then that would be an object implementing the - NodeList - interface.
Do you know of any way to add methods to the prototype of the
Object returned by this call in Firefox? (Other than
Object.prototyp e).
I have never tried, but as - NodeList - maps property accessors to its -
item - method I don't expect you will successfully extend that object.

On the other hand, why? You can write a function that takes a -
NodeList - as an argument and do anything to/with that - NodeList - that
you could with a method.

Richard.

Feb 5 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1785
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 (attrs) { for (key in attrs) { if (key == 'class') { e.className = attrs; } else if (key == 'id') { e.id = attrs;
4
2248
by: jemptymethod | last post by:
http://htmatters.net/htm/1/2006/01/EIBTI-for-Javascript-explicit-is-better-than-implicit.cfm
1
8645
by: Grzegorz ¦lusarek | last post by:
Hi all. Is it posible to add a method to prototype of object HTMLCollection? I mean something like this: HTMLCollection.prototype.indexOf=function(obj){ var result=-1; for (var i=0;i<this.length;i++){ if (this==obj){ result=i; break; } }
2
5339
by: sp1d3rx | last post by:
-----------code:------------- (whitesmiths indentation) function PopulateUlList() { alert("started populating..."); var ulz = inter.getElementsByTagName("ul"); alert(ulz.length); for(x in ulz) { alert(ulz.id);
4
7713
by: ext237 | last post by:
Simple ajax call seems to have some issues in Firefox. The "onComplete:" is called BEFORE the response is returned by the call. Is there a coding issue or a work around? var ajax = new Ajax.Request( url, {method: 'post', parameters: params, onComplete: evalInfo }); function evalInfo( request ) { // do stuff with request
3
3593
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: Ajax.Base = function() {}; Ajax.Base.prototype = { setOptions: function(options) { <...>
3
6162
by: Rudi Hausmann | last post by:
Hi! I create HTML pages on the server side. I want to extend DIV elements with more information. E.g.: <DIV id="Joe" /> Now I want Joe to have an age for example and access it using:
2
1710
by: jidixuelang | last post by:
As I know,it's not well to extend Object.prototype derictly. In the Prototype(JS Framewoke),there is no extend Object.prototype. It only add some static method for Object class. I want to konw the reason.Who can give me some advise!?
11
4209
Dormilich
by: Dormilich | last post by:
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...
0
10670
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9225
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7686
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5708
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3886
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.