0

When i put

<script>
  var renderCaptchas = function() {
    grecaptcha.render(this, {
      'sitekey' : {{ google_recaptcha_site_key }}
    });
  };

  var onloadCaptchaCallback = function () {
    Array.prototype.forEach.call(document.querySelectorAll('.g-recaptcha'),
     renderCaptchas);
  };
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCaptchaCallback&render=explicit" async defer></script>

I have this error :

reCAPTCHA couldn't find user-provided function: onloadCaptchaCallback

The function is yet well defined ... If i remove

grecaptcha.render(this, {
  'sitekey' : {{ google_recaptcha_site_key }}
});

Then it finds the callback ... I use the Google ReCaptcha v2, with the checkbox "mode".

lionelp
  • 443
  • 1
  • 7
  • 21
  • There is multiple responses of similar question here : https://stackoverflow.com/questions/1241947/how-do-i-show-multiple-recaptchas-on-a-single-page – Shim-Sao Nov 24 '18 at 11:05
  • If you use JQuery, you should add : $(document).ready(function() { ... }); – Shim-Sao Nov 24 '18 at 11:09
  • If you use prototype : document.observe("dom:ready", function() { ... }); – Shim-Sao Nov 24 '18 at 11:15
  • I have already tested the DOMContentLoaded event with no success. The link provided suggests to use javascript and explicit rendering ...and it is what i am doing here. – lionelp Nov 24 '18 at 11:21

1 Answers1

1

There is something strange in your loop for me and grecaptcha.render(this !? this = renderCaptchas ... no !? So the element you want to render is not good because it is not a valid html container.

<script>
var renderCaptchas = function(element) {
    grecaptcha.render(element, {
      'sitekey' : 'google_recaptcha_site_key'
    });
  };
</script>

or something like :

<script>
    var onloadCaptchaCallback = function () {
        [].forEach.call(document.querySelectorAll('.g-recaptcha'), function(element){
            grecaptcha.render(element, {
                'sitekey' : 'google_recaptcha_site_key'
            });
         }
     });
};
</script>

How to loop over document.querySelectorAll :

How to loop through selected elements with document.querySelectorAll

Shim-Sao
  • 2,026
  • 2
  • 18
  • 23