首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态加载reCAPTCHA

动态加载reCAPTCHA
EN

Stack Overflow用户
提问于 2016-02-07 03:58:27
回答 6查看 28.7K关注 0票数 15

使用javascript加载reCAPTCHA有多种方法,如下所示:

代码语言:javascript
复制
<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>

这段代码在页面加载时加载captcha。我想在点击"MYBTN“时加载reCAPTCHA。因此代码更改为:

代码语言:javascript
复制
<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>

但是当我点击"MYBTN“和reCAPTCHA not load时,这段代码不起作用。请帮帮我。谢谢。

EN

回答 6

Stack Overflow用户

发布于 2016-02-07 04:05:41

您只需要调用loadCaptcha()

代码语言: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);
        }
      });
    };
    loadCaptcha(); // THIS LINE WAS MISSING
 });
代码语言:javascript
复制
<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>

票数 14
EN

Stack Overflow用户

发布于 2018-01-17 17:08:48

简单情况

Html

代码语言:javascript
复制
<input type="button" id="MYBTN" value="create captcha"> 
<div id="captcha_container"></div>

JS

代码语言:javascript
复制
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);
});
票数 9
EN

Stack Overflow用户

发布于 2018-01-05 19:55:31

您刚刚在您的嵌入式脚本中提到了onload。您可以从嵌入式脚本中删除onload,也可以将代码保留在函数名loadCaptcha中的onclick事件之外。

1.第一个解决方案:

代码语言: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 src="https://www.google.com/recaptcha/api.js?render=explicit"></script>

2.第二个解决方案

代码语言:javascript
复制
<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>
<script src="https://www.google.com/recaptcha/api.js?onload=loadCaptcha&render=explicit" async defer></script>

在第一个解决方案中,当您单击按钮时,您的代码将工作。即使你不需要把then放在loadCaptcha函数中,你也可以直接调用grecaptcha.render。

但是当您在脚本标记中提到onload时,它将不会根据您的单击而工作,然后它会找到您在脚本中提到的回调函数。当您在onload of script中编写loadCaptcha时,您在onClick事件中编写了此函数。当脚本标记执行时,代码试图查找直到脚本标记执行才初始化的loadCaptcha函数(就像它在单击事件时初始化一样),因此您的脚本无法工作。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35245768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档