4

我正在尝试使用谷歌的能力来检查页面请求是来自人类还是机器人。通过这个,我想包含一个不可见的验证码,并在页面完成加载后立即提交验证。我已经浏览了谷歌文档https://developers.google.com/recaptcha/docs/invisible,正常的例子对我来说很好。但是我正在尝试以一种 gr​​ecaptcha.execute() 在页面加载后立即触发的方式对其进行调整,我尝试使用 window.onload 或 (window).ready() 并且我得到了相同的错误

grecaptcha is not defined

当然我相信是因为 grecaptcha 脚本没有准备好,实际上如果我设置了 1 秒或 2 秒的超时时间,它会执行得很好。

我尝试将“onload”参数添加到 api.js,但没有成功……顺便说一下,api.js 只是在 HEAD“recaptcha__en.js”中包含了实际的 recaptcha 脚本

我不需要实际的表格或提交任何联系信息,只需要验证查看页面的主题是人还是机器人

任何想法/建议将不胜感激!谢谢!!

编辑:按要求添加代码:

<html>
<head>
    <script>
        //called only when I set the timeout
        function onSubmit(){
            console.log("Submitted")
        }

        //not being called by the "onload parameter of the api.js script"
        function loadCaptcha(){
          console.log("loaded")
          setTimeout(function(){grecaptcha.execute()},2000)
        }

    </script>
    <script src='https://www.google.com/recaptcha/api.js' async defer></script>
    <!--<script src='https://www.google.com/recaptcha/api.js?onload="loadCaptcha"' async defer></script>----it doesnt make any difference for my case-->
</head>
<body>
<div class="g-recaptcha"
      data-sitekey="xxxxxxxxxxxxxxxxxxxxxxxxxx"
      data-callback="onSubmit"
      data-size="invisible">
</div>
<script>
    //Window.onload = setTimeout(function(){grecaptcha.execute()},2000) this works!
    Window.onload = grecaptcha.execute() //----this doesn't work---//

</script
</body>

4

2 回答 2

5

您可以将参数设置为定义 onload 回调的 url:

<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback">
</script>
<script>
function onloadCallback(){
  grecaptcha.execute();
 }
</script>
于 2017-05-06T17:55:52.820 回答
1

哦!!我刚开始使用它

$(window).on('load',function(){
    grecaptcha.execute()
})

感谢@jonasw 指出问题出在 window.onload 事件 =D

于 2017-05-06T19:05:26.270 回答