Forum Moderators: open

Message Too Old, No Replies

Check if onchange is defined in IE and FF

IE says typeof=="object", FF typeof=="undefined"

         

heisters

4:16 pm on Jun 6, 2006 (gmt 0)

10+ Year Member



Ok, I am positive that this has been covered before, and I'm sure I've seen solutions, I just can't turn any of them up at the moment.

I want to conditionally assign an event, which I do for FF like this (using Prototype):


if (typeof(this.field.onchange) == 'undefined') {
this.field.onchange = this.validate.bindAsEventListener(this);
}

However, this doesn't work in IE, because, according to my debugging, IE sees the typeof this.field.onchange as "object". How do I check if the onchange event has a function assigned? Is there an IE only isFunction() or something like that? If so, I could just put it in a Prototype try{} block.

Thanks much, and sorry for the (probably) stupid question.

Fotiman

4:29 pm on Jun 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Maybe something like this:

if ( (typeof(this.field.onchange) == 'undefined' ) ¦¦ (this.field.onchange == null) ) {

heisters

5:11 pm on Jun 6, 2006 (gmt 0)

10+ Year Member



yeah, that did the trick. Thanks.

Actually, FF likes that approach as well, so a simple


if (this.field.onchange == null) {

will suffice. Not sure why I got it in my head that I needed to test for typeof == 'undefined' instead of just null.

Thanks again.