Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicitly Rendering ReCaptcha - Onload Function Not Firing

From the documentation I understood that in order to change the language of the recaptcha I have to render it explicitly.

The problem is, however, that it's not really showing up, and the onload is not even called.
When I try to render it automatically it does work.

Here's the code:
In the HTML head: (I have also tried putting this at the end of the body tag)

<script src="https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit&hl=iw" async defer></script>

In the HTML form:

<div id="recaptcha"></div>

Javascript:

var recaptchaCallback = function() {
  console.log('recaptcha is ready'); // not showing
  grecaptcha.render("recaptcha", {
    sitekey: 'My Site Key',
    callback: function() {
      console.log('recaptcha callback');
    }
  });
}
like image 641
Gofilord Avatar asked May 15 '15 14:05

Gofilord


People also ask

Why reCAPTCHA is not showing sometimes?

One of the most common reasons why this error occurs is that of an outdated Chrome version. reCAPTCHA will actively look at the browser version before allowing you access. This is applicable to all browser versions, not just Chrome. In this case, the solution is to update Google Chrome to the latest version.

Can I run reCAPTCHA v2 and v3 on the same page?

Can I run reCAPTCHA v2 and v3 on the same page? To do this, load the v3 site key as documented, and then explicitly render v2 using grecaptcha.render.

What does no reCAPTCHA clients exist mean?

The error in console “Uncaught Error: No reCAPTCHA clients exist.” usually means that the API key is incorrect or that the scripts are not loaded in proper order.


1 Answers

Make sure that your onload method is defined before the recaptcha script. Otherwise you will have a race condition where the recaptcha script could be attempting to call your method before it is defined (especially if the recaptcha script is cached).

From the documentation for onload https://developers.google.com/recaptcha/docs/display

Note: your onload callback function must be defined before the reCAPTCHA API loads. To ensure there are no race conditions:

  • order your scripts with the callback first, and then reCAPTCHA
  • use the async and defer parameters in the script tags

For example:

<div id="recaptcha"></div>


<script type="text/javascript">
  var recaptchaCallback = function () {
    console.log('recaptcha is ready'); // not showing
    grecaptcha.render("recaptcha", {
        sitekey: 'SITE_KEY',
        callback: function () {
            console.log('recaptcha callback');
        }
    });
  }
</script>

<script src="https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit&hl=iw" async defer></script>
like image 117
Aaron Avatar answered Sep 18 '22 20:09

Aaron