views:

373

answers:

2

Is there a cross-browser way from a javascript event handler to determine whether the event has been cancelled by a previous handler? IE and other browsers like Chrome etc. have an event.returnValue property that can be tested, but FireFox doesn't appear to have any such property.

Here's an example of the scenario I'm talking about. You have a form like so:

<form id="test" name="test" onsubmit="return false;" />

And you hook up an event to it in javascript, with addEventListener/attachEvent (hookup ommitted for brevity). The event needs to know if anything prior in the chain returned false or otherwise canceled it. The IE version is like so:

function onSubmitHandler(e) {
    alert("event was cancelled prior to this handler: " + e.returnValue);
}

This works in IE, Chrome, etc., but of course not in FireFox as it doesn't have event.returnValue. I've combed the intarwebs for any other way to determine this in FireFox and come up with nothing. Is there a solution for this problem that doesn't involve setting the onsubmit attribute of the form directly?

A: 

My understanding is that the primary use of event.returnValue is to prevent the default action for that event (i.e. canceling it). For example, in a handler like yours above:

function onSubmitHandler(e) {
    e.returnValue = false;
}

The above code in IE would prevent the default action, preventing the form from being submitted. The equivalent code in Firefox, according to the Gecko DOM Reference, is:

e.preventDefault();

If you want to check if an event has been canceled, and do so in a cross-browser way, would using a library like jQuery be an option? Using jQuery to bind a function to the onsubmit event of the form and have that handler call .preventDefault() would accomplish the same thing across all supported browsers. Further, you could then use the .isDefaultPrevented() method of the jQuery event object to determine if the event has been canceled, presumably accomplishing what you are trying for with lhs = e.returnValue.

Stuart Childs
I realize what needs to be done to cancel an event, but that's not what I'm trying to do here. Using jQuery isn't an option here, as I'm writing a library that needs to be able to integrate with any other code, for example ASP.NET validation, not just stuff that uses jQuery.
Chris Hynes
I looked at isDefaultPrevented (it's a DOM 3 standard), but of course DOM 3 events aren't implemented in FireFox, so that's a no-go too.
Chris Hynes
If that's the case, I can only recommend looking at the code for jQuery's (or any other cross-browser library for that matter) preventDefault() which is exactly as I described. First it checks if preventDefault is available and if not uses IE's returnValue = false. Then just keep track elsewhere.
Stuart Childs
There's unfortunately no way to keep track elsewhere, because in Firefox you don't get notified of the value in the first place. It's basically a Firefox design flaw that makes this scenario impossible, as I see it.
Chris Hynes
A: 

Here's the kludge way around this. I was hoping for a clean standards compliant method, but it appears there is none.

function onSubmitHandler()
{
    // only called if the other onsubmit handlers succeed
}

// hook in submit
var originalOnSubmit = form.onsubmit;

form.onsubmit = function(e)
{
    var ret;

    if (originalOnSubmit)
        ret = originalOnSubmit();

    if (ret != false)
    {
        return onSubmitHandler(e);
    }
};
Chris Hynes