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?
views:
50answers:
3
+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
2010-01-08 22:25:08
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
2010-01-08 22:28:04
+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
2010-01-08 22:28:55