Reputation: 308
I was trying to implement recaptcha on my website, so I created a callback function using javascript which should be executed when a user submits a successful captcha response:
<script>
function onSubmit = function(token) {
document.getElementById("form-signin").submit();
}
</script>
But always got this error SyntaxError: missing ( before formal parameters
when using Firefox.
When using Google Chrome, I got this error instead: Uncaught SyntaxError: Unexpected token =
Both browsers indicate that the error is located at function onSubmit = function(token) {
, but I don't know why this happened.
Can anyone be so kind as to tell me what is wrong?
Upvotes: 3
Views: 9003
Reputation: 943220
function some_identifier
can start either a function declaration or a function expression. Either way, the next characters must be (
then any argument definitions, then )
.
If you want to assign the result of evaluating a function expression to a variable, as you are trying to do here, then you must declare the variable in the normal way (i.e. with the var
keyword, unless you have declared it already, or are not using strict mode and want to create a global).
So either:
function onSubmit(token) {
document.getElementById("form-signin").submit();
}
or
var onSubmit = function(token) {
document.getElementById("form-signin").submit();
}
Upvotes: 2
Reputation: 7963
There are two basic ways to declare a named function:
function onSubmit(token){
document.getElementById("form-signin").submit();
}
Or:
var onSubmit = function(token){
document.getElementById("form-signin").submit();
};
In your case, just go with the first.
Upvotes: 2