javascript - SyntaxError: missing ( before formal parameters - Stack Overflow

I was trying to implement recaptcha on my website, so I created a callback function using javascript wh

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?

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?

Share Improve this question asked Jan 7, 2017 at 13:47 GPrazGPraz 3084 silver badges13 bronze badges 2
  • you should try var onSubmit = – Deep Commented Jan 7, 2017 at 13:49
  • How I got this error: I didn't get enough sleep and for some reason I though I can use dashes (mathematical operators for subtraction) in function names. For anyone wondering and stumbling upon the same error, you can't (obviously; it's subtraction). – jg6 Commented Aug 25, 2020 at 21:03
Add a ment  | 

2 Answers 2

Reset to default 2

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.

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();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744334337a4569037.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信