I need to test whether the value of a form's onsubmit
is a function. The format is typically onsubmit="return valid();"
. Is there a way to tell if this is a function, and if it's callable? Using typeof just returns that it's a string, which doesn't help me much.
EDIT: Of course, I understand that "return valid();" is a string. I've replace
d it down to "valid();", and even "valid()". I want to know if either of those is a function.
EDIT: Here's some code, which may help explain my problem:
$("a.button").parents("form").submit(function() {
var submit_function = $("a.button").parents("form").attr("onsubmit");
if ( submit_function && typeof( submit_function.replace(/return /,"") ) == 'function' ) {
return eval(submit_function.replace(/return /,""));
} else {
alert("onSubmit is not a function.\n\nIs the script included?"); return false;
}
} );
EDIT 2: Here's the new code. It seems that I still have to use an eval, because calling form.submit() doesn't fire existing onsubmits.
var formObj = $("a.button").parents("form");
formObj.submit(function() {
if ( formObj[0].onsubmit && typeof( formObj.onsubmit ) == 'function' ) {
return eval(formObj.attr("onsubmit").replace(/return /,""));
} else {
alert("onSubmit is not a function.\n\nIs the script included?");
return false;
}
} );
Suggestions on possibly how to do this better?
You could simply use the
typeof
operator along with a ternary operator for short:If it is a function we call it and return it's return value, otherwise just return
true
Edit:
I'm not quite sure what you really want to do, but I'll try to explain what might be happening.
When you declare your
onsubmit
code within your html, it gets turned into a function and thus its callable from the JavaScript "world". That means that those two methods are equivalent:These two will be both functions and both will be callable. You can test any of those using the
typeof
operator which should yeld the same result:"function"
.Now if you assign a string to the "onsubmit" property via JavaScript, it will remain a string, hence not callable. Notice that if you apply the
typeof
operator against it, you'll get"string"
instead of"function"
.I hope this might clarify a few things. Then again, if you want to know if such property (or any identifier for the matter) is a function and callable, the
typeof
operator should do the trick. Although I'm not sure if it works properly across multiple frames.Cheers
using a string based variable as example and making use
instanceof Function
You register the function..assign the variable...check the variable is the name of function...do pre-process... assign the function to new var...then call the function.form.onsubmit will always be a function when defined as an attribute of HTML the form element. It's some sort of anonymous function attached to an HTML element, which has the this pointer bound to that FORM element and also has a parameter named
event
which will contain data about the submit event.Under these circumstances I don't understand how you got a string as a result of a typeof operation. You should give more details, better some code.
Edit (as a response to your second edit):
I believe the handler attached to the HTML attribute will execute regardless of the above code. Further more, you could try to stop it somehow, but, it appears that FF 3, IE 8, Chrome 2 and Opera 9 are executing the HTML attribute handler in the first place and then the one attached (I didn't tested with jQuery though, but with addEventListener and attachEvent). So... what are you trying to accomplish exactly?
By the way, your code isn't working because your regular expression will extract the string "valid();", which is definitely not a function.
If it's a string, you could assume / hope it's always of the form
parse for the function name, and then see if that function is defined using
EDIT : missing parameter f in function testOnsubmitAndSubmit
The above should work regardless of whether you assign the
onsubmit
HTML attribute or assign it in JavaScript: