How to modify firstChild property for FF?

S

szimek

Hi!

Like many others before recently I've found out that firstChild works
little bit differently in IE and other browsers. I've got huge app
that is currently IE only and used firstChild all over the place.
Currently I wrote my own function firstNonTextChild(element), but it
would be much more convenient for me to modify element.firstChild
directly for FF and others, so I wouldn't have to change the code.

The problem is that my current function looks like this:
function firstNonTextChild(element) {
var e = element.firstChild;
// Fetch next element unless non-text node is fetched
while(e.nodeType == 3) {
e = e.nextSibling;
}
return e;
}
As you can see it gets firstChild property itself. Is it possible to
make an alias or something for the original firstChild property, so I
could do
HTMLElement.prototype.firstChild = function() {
....
element.originalFirstChild;
....
}
And one more thing, my "solution" creates a method, not a property
like the original firstChild. How can I modify this code to make
modified firstChild a property and not a function?
 
W

webbugtrack

[clipped]
so I
could do
HTMLElement.prototype.firstChild = function() {
...
element.originalFirstChild;
...}

And one more thing, my "solution" creates a method, not a property
like the original firstChild. How can I modify this code to make
modified firstChild a property and not a function?

Your out of luck on this. It would be handy, but as you've noted you
would need to prototype a method/property on the HTMLElement.

This will work in some browsers, but IE is not one of them (even IE7).

http://webbugtrack.blogspot.com/2007/08/bug-186-cant-prototype-on-htmlelements.html

Your best bet at the moment, is to keep the function you have, or if
you must, prototype the .firstChild property on Firefox only, to work
like IE's implementation. However I would be extremely weary of doing
this.
 
S

szimek

[clipped]
so I
could do
HTMLElement.prototype.firstChild = function() {
...
element.originalFirstChild;
...}
And one more thing, my "solution" creates a method, not aproperty
like the original firstChild. How can I modify this code to make
modified firstChild apropertyand not a function?

Your out of luck on this.  It would be handy, but as you've noted you
would need to prototype a method/propertyon the HTMLElement.

This will work in some browsers, but IE is not one of them (even IE7).

http://webbugtrack.blogspot.com/2007/08/bug-186-cant-prototype-on-htm...

Your best bet at the moment, is to keep the function you have, or if
you must, prototype the .firstChildpropertyon Firefox only, to work
like IE's implementation. However I would be extremely weary of doing
this.
Thanks for answering!
Just one more question - how to make firstChild to be a property and
not a function?
 
J

Joost Diepenmaat

szimek said:
Thanks for answering!
Just one more question - how to make firstChild to be a property and
not a function?

In *mozilla only* <-- this means "for all intents and purposes
completetly useless on the web", or *this may possibly work somewhere*:

See defining getters and setters:

<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:
Creating_New_Objects:Defining_Getters_and_Setters>

Note that this will of course still call a method, it'll just look like
it doesn't (which is fair enough).

Note also (from that page):

"Prior Firefox 3.0 getter and setter are not supported for DOM
Elements. Older versions of Firefox silently fail."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top