[Javascript] Re: Re: Is this right Javascript?
David T. Lovering
dlovering at gazos.com
Wed Apr 9 10:56:55 CDT 2003
Hmmm... fairly eclectic coding here...
First off, every function (even a prototype constructor) must have a name.
'function' by itself won't cut it.
Second, in this context, 'window' and 'document' point to the same thing.
Third, 'onkeydown' is undefined for normal JavaScript (although it may have
some bizarre other-worldly existence in M$oft's bastardized JScript).
I suspect the event you were attempting to intercept was 'onclick', which
IS defined, and actually makes sense for a checkbox.
I hope "SetFocus" is something you've defined, as I don't believe it exists
in DOM-standard JavaScript. If what you want to do is set the focus on the
checkbox, you don't need it (as noted earlier), as the act of clicking on it
puts it there anyway.
If you are an old-timey X-windows programmer (or even an early Java programmer)
you probably recall that you had to manipulate the focus depending on events
in the window in order to 'look at' the object you clicked on. [That's less
true today]. However, JavaScript doesn't require it. It won't hurt mind you,
it's just that it doesn't actually DO anything different.
Since the first code snippet doesn't make sense (at least to my diminished
sensibilities), I can't say whether it SHOULD do the same as the second
snippet. Moreover, the second code snippet just paints a checkbox which just
sits there and lets you click on it until the cows come home. Nothing wrong
with it, just nothing very useful.
That said, exactly what were you trying to do? If you want to build a checkbox
handler, it can be done thusly:
<script language='JavaScript'>
<!--
var theObject;
function checkBoxHandler(theObject) {
if (!theObject) { return -1; }
if (theObject == '') { return -1; }
if (theObject.nodeName != 'INPUT' || theObject.type != 'checkbox') { return -1; }
// ... the above bits make certain you really HAVE passed this routine a pointer to a checkbox
var theState = theObject.checked;
var theValue = theObject.value;
var theName = theObject.name;
alert('checkbox \'' + theName +
'\nhas value \'' + theValue +
'\nin state \'' + theState);
}
// -->
</script>
......
<form name="formulario">
<input type="checkbox" name="richtext" value="selected" onClick='checkBoxHandler(this)'>
</form>
Of course, my grey cells could all be on vacation...
-- Dave Lovering
> Is this:
> function
> window.document.formulario.richtext.onkeydown() {
> return SetFocus();
> }
>
> ....the same as this:
> <form name=formulario>
> <input type="checkbox" name="richtext"
> onkeydown="foo();">
> <form>
> <script>
> function foo(){
> return SetFocus();
> }
> </script>
More information about the Javascript
mailing list