views:

136

answers:

3

Here's the test page I'm using. This version works fine, forwarding to #success:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html><head>
<script type="text/javascript" src="prototype.js"></script>
</head><body>

<form id='form' method='POST' action='#fail'>
    <button id='button'>Oh my giddy aunt!</button>

    <script type="text/javascript">
    var fn = function() {
        $('form').action = "#success";
        $('form').submit();
    }
    $('button').observe('mousedown', fn);
    </script>
</form>

</body></html>

If I empty the handler:

var fn = function() {
}

The form is submitted, but of course we are sent to #fail this time.

With an alert in the handler:

var fn = function() {
    alert("omg!");
}

The form is not submitted. This is awfully curious.

With event.stop(), which is supposed to prevent the browser taking the default action:

var fn = function(event) {
    event.stop();
}

We are sent to #fail.

So alert() is more effective at preventing a submission than event.stop(). What gives?

I'm using Firefox 3.6.3 and Prototype 1.6.0.3. This behaviour also appears in Prototype 1.6.1.

+2  A: 

The reason that you are not able to stop the form submission using Event.stop(event) is because the event you are observing is the mousedown event, not a form.submit() event. I'm guessing the reason that the alert() prevents submission is because you have overwritten the default behaviour of mousedown on an input type="button"

seengee
Close, it seems the reason the form submission continued was because I wasn't also catching click. I still don't see the relevance of observing (not overwriting) mousedown has to alert().
lxs
A: 

it looks like the 'e' in your event.stop needs to be capitalized (to read Event.stop(); )

KevinDeus
I think that's true on IE, in FF events are passed in as arguments so the capitalisation should match the formal parameter, as it does :)
lxs
A: 

It seems the reason the form submission continued was because I wasn't also catching click.

lxs