Cancelling a pending onblur event

D

Dave Hammond

Hi All,

I have a web form which performs certain actions upon moving focus away
from a field. However, if the user clicks the top corner 'X' icon to
close the window, the onBlur event still fires. If, for example, the
onBlur event was an alert() popup: when the user clicks the close
window icon, the window closes and then the alert pops up. Clearly, if
the user closed the window, there is no point to performing the onBlur
event.

Is there any way to detect that an onBlur event is pending, then do
something in an onUnload handler to cancel that event? Perhaps there
is an event queue which can be manipulated?

Thanks in advance.

-Dave H.
 
D

Dave Hammond

Thanks for your reply. Changing the event handler isn't an option. The
application is a grid-style data editor which uses the onblur handler
to validate data whenever the user moves the cursor. using onchange
won't work because the validation needs to happen, even if the user
doesn't change the data (ie, leaving an empty field empty would not
trigger an onchange), and the data has to be validated in realtime, not
when they submit.
 
M

Michael Winter

On 23/06/2005 17:05, Dave Hammond wrote:

[snip]
Is there any way to detect that an onBlur event is pending, then do
something in an onUnload handler to cancel that event? [...]

No, because the blur event won't be pending: it will have been handled
already. You have the order of events confused.

Mike
 
J

Jc

Dave said:
Thanks for your reply. Changing the event handler isn't an option. The
application is a grid-style data editor which uses the onblur handler
to validate data whenever the user moves the cursor. using onchange
won't work because the validation needs to happen, even if the user
doesn't change the data (ie, leaving an empty field empty would not
trigger an onchange), and the data has to be validated in realtime, not
when they submit.

You may want to consider using a different technique, which should
still satisfy your real-time requirement. For example, a technique I've
used before was to show static data until the user clicked on a cell,
which would "activate" the row, changing each cell into an editable
entity, such as a drop-down, editbox, etc. Once the user was done
editing that row, they could click the checkmark button at the
beginning of the row to submit the changes behind the scenes to the
server. Clicking on another row without submitting the changes would
prompt the user to either save or discard the changes.

I recommend avoiding onblur if you can (especially for input
validation), you will continue to run into these sort of issues, some
of which I haven't seen a workaround for, other than to simply not use
onblur for that particular purpose.

Another way to avoid using onblur is to detect when the user has
switched to a new cell, you can detect a TAB in onkeydown, and you can
detect a click in onclick, so you should be able to determine when they
have switched cells, as long as you maintain a little bit of state
information (eg. the active cell).

So simply putting focus on something validates it? Let's hope your
users don't accidentally hold down the TAB key.
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top