最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Invisible captcha executed as soon as page loads - Stack Overflow

programmeradmin2浏览0评论

I'm trying to use the ability of google to check if the page request es from a human or bot. By this I want to include an invisible captcha and submit the verification as soon as the page finishes loading. I've gone through the google docs and the normal example works fine for me. But I'm trying to tweak it in a way the grecaptcha.execute() fires off as soon as the page loads, I've tryied with window.onload, or (window).ready() and I get the same error of

grecaptcha is not defined

Of course I believe it is because the grecaptcha script is not ready, in fact if I set a timeout of 1sec or 2 secs, it will execute fine.

I tried to add the "onload" parameter to the api.js with no luck... and by the way the api.js just includes the actual recaptcha script in the HEAD "recaptcha__en.js"

I do not need an actual FORM or submit any contact information, just need to verify if the subject viewing the page is a human or a bot

any ideas/suggestions will be appreciated! thanks!!

EDIT: adding code as requested:

<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='.js' async defer></script>
    <!--<script src='.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>

I'm trying to use the ability of google to check if the page request es from a human or bot. By this I want to include an invisible captcha and submit the verification as soon as the page finishes loading. I've gone through the google docs https://developers.google./recaptcha/docs/invisible and the normal example works fine for me. But I'm trying to tweak it in a way the grecaptcha.execute() fires off as soon as the page loads, I've tryied with window.onload, or (window).ready() and I get the same error of

grecaptcha is not defined

Of course I believe it is because the grecaptcha script is not ready, in fact if I set a timeout of 1sec or 2 secs, it will execute fine.

I tried to add the "onload" parameter to the api.js with no luck... and by the way the api.js just includes the actual recaptcha script in the HEAD "recaptcha__en.js"

I do not need an actual FORM or submit any contact information, just need to verify if the subject viewing the page is a human or a bot

any ideas/suggestions will be appreciated! thanks!!

EDIT: adding code as requested:

<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./recaptcha/api.js' async defer></script>
    <!--<script src='https://www.google./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>

Share Improve this question edited May 6, 2017 at 17:55 Bucci83 asked May 6, 2017 at 17:43 Bucci83Bucci83 951 gold badge4 silver badges10 bronze badges 6
  • please share code what you have tried so far and screenshot imges if possible – Naga Sai A Commented May 6, 2017 at 17:47
  • @Jonasw if I use "render=explicit" I have to use the render() method which will make the recaptcha visible, I need it to be invisible. – Bucci83 Commented May 6, 2017 at 17:58
  • @Bucci83 okay but the onload should still work shouldnt it? – Jonas Wilms Commented May 6, 2017 at 17:59
  • @Bucci83 Window.onload = grecaptcha.execute() will execute right now, you want window.onload = ()=>grecaptcha.execute(); – Jonas Wilms Commented May 6, 2017 at 18:00
  • I chaged it to ()=>grecaptcha.execute() and it is not running... any suggestions? you say window.onload = grecaptcha.execute() will execute as soon as the parser reaches the code, how can I fix it? – Bucci83 Commented May 6, 2017 at 18:37
 |  Show 1 more ment

2 Answers 2

Reset to default 5

You can set a param to the url defining an onload callback:

<script src="https://www.google./recaptcha/api.js?onload=onloadCallback">
</script>
<script>
function onloadCallback(){
  grecaptcha.execute();
 }
</script>

Ohh!! I just got it to work using

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

thanks @jonasw to point out the issue was on the window.onload event =D

发布评论

评论列表(0)

  1. 暂无评论