Sylar
Sylar

Reputation: 12082

Render Google Recaptcha Without Callback

I want to render the recaptcha element without the onload callback. I have a page that has no <form>. They use jquery to get the value from the input field before saving to database so I want to get a response before saving.

HTML head:

<script src='https://www.google.com/recaptcha/api.js'></script>

BODY:

<div id="googleQuoteRecapture"></div>

JS inside document.ready:

var widget = document.getElementById('googleQuoteRecapture')
var widgetId1 = grecaptcha.render(widget, {
  'sitekey' : 'site_key',
  'theme' : 'light'
});

Uncaught ReferenceError: grecaptcha is not defined

The aim is to grecaptcha.getResponse(widgetId1)

Is this possible?

Upvotes: 0

Views: 1763

Answers (1)

Titus
Titus

Reputation: 22474

You can use an interval to check for the presence of the grecaptcha variable.

Here is an example:

var interval = setInterval(function(){
  if(window.grecaptcha){
    var widget = document.getElementById('googleQuoteRecapture')
    var widgetId1 = grecaptcha.render(widget, {
      'sitekey' : 'site_key',
      'theme' : 'light'
    });
    clearInterval(interval);
  }
}, 100);

But is much easer to use the onloadCallback parameter.

Upvotes: 3

Related Questions