4

I'm trying to use the ability of google to check if the page request comes 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.com/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.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>

Bucci83
  • 95
  • 1
  • 3
  • 10
  • please share code what you have tried so far and screenshot imges if possible – Naga Sai A May 06 '17 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 May 06 '17 at 17:58
  • @Bucci83 okay but the onload should still work shouldnt it? – Jonas Wilms May 06 '17 at 17:59
  • @Bucci83 *Window.onload = grecaptcha.execute()* will execute right now, you want *window.onload = ()=>grecaptcha.execute();* – Jonas Wilms May 06 '17 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 May 06 '17 at 18:37
  • added the jquery way (function(){grecaptcha.execute()}), not working either :( – Bucci83 May 06 '17 at 18:57

2 Answers2

5

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

<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback">
</script>
<script>
function onloadCallback(){
  grecaptcha.execute();
 }
</script>
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Hi, yes I tried, but is not being called :( I added a simple console.out and I don't see it called, it works when I add the render=explicit param but that requires me to add the render() method aswell, so it will add the recaptcha checkbox, and I don't want that... – Bucci83 May 06 '17 at 18:06
  • @Bucci83 i think that the recaptcha traces user moves, so there must be a visiting time on that page and some userinteraction... – Jonas Wilms May 06 '17 at 18:07
1

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

Bucci83
  • 95
  • 1
  • 3
  • 10
  • ...of course it is not working on IE11... in any case, now I'm getting the recaptcha challenge of selecting images I guess it is too fast the execution and the API doesn't know if it is human or robot so it asks for image selection, I guess I'll need to figure out some other way... – Bucci83 May 06 '17 at 20:32
  • How did you fix this finally? I want to do same to avoid the timeout, but don't want the images..... – Helenp May 01 '19 at 09:21