Re: questions about form action= and onsubmit() handling
- From: Conrad Lender <crlender@xxxxxxxxx>
- Date: Wed, 31 Dec 2008 07:51:15 +0100
On 2008-12-31 06:45, William Gill wrote:
I dusted off an old JS reference (it's been years) to work on a HTML
database edit app, and have a couple of questions. I have noticed that
form action="javascript:myscript(this)" and onSubmit="myscript(this)"
send different objects to the script. I think IE and Firefox get
different objects (Maybe the document object and window object ?) from
the action=, but both seem to get the form object from onSubmit. Can
anyone expound on what I'm seeing? Is there a good way to see exactly
what object myscript(this) gets? I have cobbled a "dump to popup window
routine", but it's pretty crude.
The most convenient way I can think of is the Firebug add-on for
Firefox. With Firebug, you can just write console.log(myMysteryObject)
and you will see the object's type, properties and methods. Firebug is
also available for other browsers as "Firebug Lite".
In your case, this will show you that |this| refers to the global object
(window) for javascript:myscript(this), and to the form element for
onsubmit="myscript(this)". In the latter case the debug message will
quickly disappear because the form gets submitted (see below).
I'd very much recommend against using the "javascript:" pseudo-protocol.
The only use I've ever found for it is in bookmarklets, for everything
else there's always a better solution.
... Here again I'm
having problems. A reset button works exactly as advertised, but I'm
having a couple of problems with the other buttons like "Update All" and
"Delete Record." The first problem is determining which button was
clicked. I could use each button's onClick=myscript(this) property then
in myscript(caller) use caller.name to see which button then caller.form
to get the form's elements collection, loop through them checking for
className == "changed", and process the data, but this seems a long way
around.
I don't know what kind of processing and validating your script does...
I've seen worse solutions than looping through the elements and checking
their CSS classes (if they're reliable). As for which button triggered
the submit, I have to admit that I'm not sure if that's even possible
from the submit handler alone. The event object doesn't seem to contain
this information. I've recently had to deal with a form like this
(multiple submit buttons, and a single onsubmit handler), and my
solution was similar to what you suggested; I used a single click
handler for all the buttons (which don't have to be submit buttons, by
the way), and let the handler figure out which button was clicked.
Lastly, if I "submit" the form by either making the button type
submit (or by hitting return from within the form) it results in the
displayed forms being reset to original hard coded (by PHP))value=
values. The only way I seem to avoid this is setting form
action="javascript:noop(), where noop() is an empty function like:
Again, there's always a better way than "javascript:". If your form has
an onsubmit attribute, use it like this:
<form action="(whatever)" onclick="return myscript(this)">
Then have the "myscript" function return false to prefent the default
action of the event (=submitting the form). Or just write
'onsubmit="return false"', and just handle the button clicks.
HTH
- Conrad
.
- Follow-Ups:
- Re: questions about form action= and onsubmit() handling
- From: William Gill
- Re: questions about form action= and onsubmit() handling
- From: JR
- Re: questions about form action= and onsubmit() handling
- References:
- questions about form action= and onsubmit() handling
- From: William Gill
- questions about form action= and onsubmit() handling
- Prev by Date: Re: popup window
- Next by Date: Re: popup window
- Previous by thread: questions about form action= and onsubmit() handling
- Next by thread: Re: questions about form action= and onsubmit() handling
- Index(es):
Relevant Pages
|