By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Key event processing : return value

P: n/a
X-No-Archive: yes
>From javascript examples I learned that when you set an onkey event to
a function, the return value of the function is a boolean. What does
this boolean value mean?

Example: <body
onload="document.getElementById('field').onkeydown =function(x) { /* do
something */; return false; };">

Bonus question: What search keywords could I use to find this
information ; all my searches turned up nothing.

Thanks!

Nov 20 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

ma*********@email.com wrote:
X-No-Archive: yes
From javascript examples I learned that when you set an onkey event to
a function, the return value of the function is a boolean. What does
this boolean value mean?
This goes back to the original Netscape days and can probably be
classed as DOM 0. If an event handler returns false, the default
action is cancelled[1] for the element that initiated the event. The
value is used by the target/source element's intrinsic event handler to
(maybe) cancel or continue with the default action for that element.
Example: <body
onload="document.getElementById('field').onkeydown =function(x) { /* do
something */; return false; };">
In the above case, the result of the keydown event (i.e. putting the
key's value into the input) will be cancelled and you won't be able to
enter anything into the input[2].

A common use is to conditionally submit a form:

HTML:
<form onsubmit="return validate(this);" ...>...</form>

SCRIPT:
function validate(form) {
var isValid = false;
// validation code to set isValid to true if everything
// is valid, otherwise leave it as false
return isValid;
}

However, it doesn't stop the event propagating and can be very
unreliable in cross-browser applications. You should really only use
it for form submission and to stop link navigation, e.g.

<a href="http://www.blah.com"
onclick="return goElsewhere();">Conditional link</a>

Bonus question: What search keywords could I use to find this
information ; all my searches turned up nothing.
"javascript event return false" should do OK. There some good stuff at
Quirksmode:

<URL: http://www.quirksmode.org/js/events_early.html >
1. The usual caveats apply: scripting must be enabled and the browser
must support this aspect of the Netscape event model - not all browsers
do.

2. Some browsers will not cancel the default action at all, others will
do so only in certain cases, and others always.

--
Rob

Nov 20 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.