tags:

views:

50

answers:

3

I would like a way of detecting/triggering a function when the form onsubmit is cancelled by any onsubmit handler. What's the most reliable method for doing this?

+1  A: 

Wrap it up...

// This code should execute last (after onsubmit was already assigned)
var oldsub = document.forms[0].onsubmit;
document.forms[0].onsubmit = function() {
  if(!oldsub)
    alert("Onsubmit did not exist!");
  else if(oldsub())
    alert("Onsubmit passed!");
  else
    alert("Onsubmit failed!");
}
Josh Stodola
A: 

I'm probably wrong, but would returning false on one handler cancel the stack? Failing that, you could attach a check call on each event to check whether another one in the stack canceled.

Kevin Peno
+1  A: 

you could override all the forms' onsubmit handlers with your own:

var f = document.getElementById('myForm'); // or whatever
if (f.onsubmit) {
    var oldSubmit = f.onsubmit;
    f.onsubmit = function () {
        var result = oldSubmit.call(this);
        if (result === false) {
            alert("Cancelled.");
        }
        return result;
    };
}
nickf