GiJeet wrote:
Hello, I'm trying to figure this code out.
<input type="submit" onclick="if(!confirm('Are you sure?') return
false; "... />
I know that if you return false in an event like this onclick it
prevents the default behavior of the event. So, in the onclick of a
button the default behavior is to submit the form.
No, the default behavior is to activate the element.
Confirm returns the value 1 if the user clicks OK and the value 0 if
the user clicks Cancel
No, it returns `true' or `false'. Boolean is a type of its own in
ECMAScript implementations.
and 1 is treated as true and 0 is treated as false right?
If you mean "evaluated in boolean expressions" by "treated", then you are
correct.
so, if the user click OK, a 1 is returned
`true' is returned.
but the ! turns it to false
Correct.
and the form should not submit even thou the user click OK,
Non sequitur. The default action of the `click' event is to activate the
element, not to submit the form. That is the default action of the form's
submit event, and so must be handled in the `onsubmit' event handler attribute.
and then there's a return false right after the confirm function.
Doesn't matter.
so what's happening here? do we even need the return false statement here?
The entire code is bogus. However, return window.confirm(...) accomplishes
the same: nothing of consequence here, preventing form submission if the
dialog is canceled within the `onsubmit' attribute value.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee