I would like to make my own website, where I use reCAPTCHA. However, I don’t know how to wait after grecaptcha.execute() until the user has completed the tasks. Because now the link is called directly without passing the tasks. For the rest I use the standard Google Script https://developers.google.com/recaptcha/docs/invisible It is the reCAPTCHA v2 invisible.
I would be happy about answers.
JavaScript
x
36
36
1
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
2
<script>
3
function onSubmit(token) {
4
grecaptcha.execute().then(var vslg = document.getElementById("vslg").value;
5
window.location.replace("url");
6
}
7
</script>
8
</head>
9
<body>
10
<a class="button"></a>
11
<div class="topBar">
12
</div>
13
<div class="underTopBar">
14
<form action="JavaScript:onSubmit()" class="flex-itemform form" method="POST" id="formV">
15
<table>
16
<tr>
17
<td>
18
<div>
19
<input type="text" id="vslg" required>
20
</div>
21
</td>
22
<td>
23
<div>
24
<div class="g-recaptcha"
25
data-sitekey="..."
26
data-callback="onSubmit"
27
data-size="invisible">
28
</div>
29
<input type="submit" class="buttonDesign" value="Senden">
30
</div>
31
</td>
32
<tr>
33
</table>
34
</form>
35
</div>
36
Advertisement
Answer
The following code does this:
- The
<button class="g-recaptcha"...
is the Automatically bind the challenge to a button. It will automatically trigger the invisible recaptcha when the button is clicked. - Once the recaptcha is completed it will add a hidden field named
g-recaptcha-response
which contains the token and then run theonSubmit
callback which submits the form.
JavaScript
1
30
30
1
<head>
2
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
3
<script>
4
function onSubmit() {
5
document.getElementById("formV").submit();
6
}
7
</script>
8
</head>
9
<body>
10
<a class="button"></a>
11
<div class="topBar">
12
</div>
13
<div class="underTopBar">
14
<form class="flex-itemform form" method="POST" id="formV">
15
<table>
16
<tr>
17
<td>
18
<div>
19
<button
20
class="g-recaptcha buttonDesign"
21
data-sitekey="..."
22
data-callback="onSubmit"
23
data-size="invisible">Senden</button>
24
</div>
25
</td>
26
<tr>
27
</table>
28
</form>
29
</div>
30
Important: You still need to verify the token g-recaptcha-response
server side. See Verifying the user’s response. Without verifying the token, adding the recaptcha to the frontend doesn’t stop anyone from submitting the form.