Search code examples
javascriptcssrecaptcha

How to automatically redirect to next page after completing reCAPTCHA without "Submit" button?


I am trying to redirect the person to a different page after captcha attempt is successful. Basically I want them to be routed to a specific page after clicking "verify" button inside captcha

I used this code, but this is not working.

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
 <script type="text/javascript"> 
 if (grecaptcha.getResponse() == "success")
  {
     function Redirect()
      {
         window.location.href = "example.com";
         }

           setTimeout(Redirect,5000);
            }
             else alert("You can't proceed!");
              </script>

  <div data-type="image" class="g-recaptcha" data-sitekey="my-sitekey"></div> 

Solution

  • You can use data-callback to specify a function to be called when the response is successful.

    <script>
    function captchaPassed() {
        location.href = "example.com";
    }
    </script>
    <div data-type="image" class="g-recaptcha" data-sitekey="my-sitekey" data-callback="captchaPassed"></div> 
    

    Note that you should still verify the token on the server.