executing a reference to field.onchange in Firefox
I have HTML code which calls a javascript function from a form, using:
<form name="f" id="f" ...>
<input name="myField" onchange="doFunct(f.myField.value,f.yourF开发者_如何转开发ield);" />
<input name="yourfield" onchange="doFunct(f.yourField.value,f.anotherField);" />
...
In the javascript code:
function doFunct(field,dest){
// do something with field
dest.value = field;
// see if the dest has a change field
if (dest.onchange !== null) {
//we have an onchange function, so let's do it!
dest.onchange();
}
}
This works fine in Safari, Opera, and Chrome. It fails in FireFox with an error:
Error: dest.onchange is not a function
Any suggestions on how to execute "dest.onchange()" from javascript in FireFox?
I need this capability to cascade changes to fields in an input form.
To execute events, do not add the 'on' prefix to it, just run dest.change();
It appears from more research, in my O'Reilly JavaScript Pocket Reference, I find there's a paragraph that states:
null
(and undefined)
The JavaScript keyword null
is a special value that indicates "no value". If a variable contains null
, you know that it does not contain a valid value of any type. There is one other special value in JavaScript: the undefined value. This is the value returned when you use an undeclared or uninitialized variable or when you use a non-existent object property. There is no JavaScript keyword for this value.
After some testing with alert(dest.onchange) I found that Firefox was not complaining about every invocation of dest.onchange()
but only those which were errors (undefined). Apparently (Grrrr!) Firefox didn't get the memo: [There is no JavaScript keyword for this value.]
If I change my code to test for dest.onchange !== undefined
then Firefox is happy, but then Safari, Opera and Chrome FAIL at the test. If I change the code as below, it works in all four browsers.
if ( (dest.onchange !== null) // test for safari, chrome, opera
&& (dest.onchange !== undefined) ) { // test for firefox
//we have an onchange function, so let's do it!
dest.onchange();
}
And I got to spend 8 hours trying to figure out why Firefox doesn't play nice.
精彩评论