Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load reCAPTCHA dynamically

There are several ways to load reCAPTCHA using javascript such as below:

<html>
  <head>
    <title>Loading captcha with JavaScript</title>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
    <script type='text/javascript'>
    var captchaContainer = null;
    var loadCaptcha = function() {
      captchaContainer = grecaptcha.render('captcha_container', {
        'sitekey' : 'Your sitekey',
        'callback' : function(response) {
          console.log(response);
        }
      });
    };
    </script>
  </head>
  <body>
      <div id="captcha_container"></div>
      <input type="button" id="MYBTN" value="MYBTN">
      <script src="https://www.google.com/recaptcha/api.js?onload=loadCaptcha&render=explicit" async defer></script>
  </body>
</html>

This code load captcha on pageload. I want load reCAPTCHA just when clicked on "MYBTN". So the code changes into:

<html>
  <head>
    <title>Loading captcha with JavaScript</title>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
    <script type='text/javascript'>
$('#MYBTN').on('click',function(){
    var captchaContainer = null;
    var loadCaptcha = function() {
      captchaContainer = grecaptcha.render('captcha_container', {
        'sitekey' : 'Your sitekey',
        'callback' : function(response) {
          console.log(response);
        }
      });
    };
 });
    </script>
  </head>
  <body>
      <div id="captcha_container"></div>
      <input type="button" id="MYBTN" value="MYBTN">
      <script src="https://www.google.com/recaptcha/api.js?onload=loadCaptcha&render=explicit" async defer></script>
  </body>
</html>

But this code didn't work when I click on "MYBTN" and reCAPTCHA not load. Help me plz. Thanks.

like image 456
Reza Avatar asked Feb 06 '16 19:02

Reza


People also ask

How do I load reCAPTCHA?

Launch Google Chrome and go to the three-dot menu. Now, click on the Help > About Chrome option. It will now search for the latest update automatically and install it. After updating Chrome, restart it and go to the reCAPTCHA site to see if it is working fine now.


2 Answers

You just need to call loadCaptcha()

$('#MYBTN').on('click',function(){
    var captchaContainer = null;
    var loadCaptcha = function() {
      captchaContainer = grecaptcha.render('captcha_container', {
        'sitekey' : 'Your sitekey',
        'callback' : function(response) {
          console.log(response);
        }
      });
    };
    loadCaptcha(); // THIS LINE WAS MISSING
 });
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
      <div id="captcha_container"></div>
      <input type="button" id="MYBTN" value="MYBTN">
      <script src="https://www.google.com/recaptcha/api.js?onload=loadCaptcha&render=explicit"></script>
like image 73
Gavriel Avatar answered Sep 18 '22 16:09

Gavriel


Simple situation

Html

<input type="button" id="MYBTN" value="create captcha"> 
<div id="captcha_container"></div>

JS

var testSitekey = '6LeLzA4UAAAAANNRnB8kePzikGgmZ53aWQiruo7O';

$('#MYBTN').click(function() {
  $('body').append($('<div id="captcha_container" class="google-cpatcha"></div>'));
  setTimeout(function() {
    grecaptcha.render('captcha_container', {
      'sitekey': testSitekey
    });
  }, 1000);
});

Online demo (jsfiddle)

like image 22
Ali Soltani Avatar answered Sep 20 '22 16:09

Ali Soltani