开发者

Interrupting Prototype handler, alert() vs event.stop()

开发者 https://www.devze.com 2022-12-26 14:50 出处:网络
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\"

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">
<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 behav开发者_如何学Pythoniour also appears in Prototype 1.6.1.


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"


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号