如何在reCAPTCHA验证后重定向?

问题描述 投票:0回答:1

我在我的网站上使用Google reCAPTCHA。如果验证码成功,我想重定向到另一个网站。

我使用的代码是:

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script type="text/javascript">
  if (grecaptcha.getResponse() == "success") {
    function Redirect() {
      window.location = "example.com";
    }
    document.write("You will be
      redirected to a new page in 5 seconds ");      setTimeout('Redirect()',
      1000);
  }
  } else {
    alert("You can't proceed!");
</script>

<body>
  <div data-type="image" class="g-recaptcha" data-sitekey="public site key"></div>
</body>

但它不起作用。

编辑

我只是不太了解JavaScript。这就是我问的原因,但仍然不理解。这意味着什么,我与它有什么关系。 https://www.google.com/recaptcha/api/siteverify你能给我一个完整代码的例子

javascript html html5
1个回答
0
投票

第一件事是如果你想重定向使用window.location.href =“example.com”,第二件事是你向用户显示该页面将在5秒内重定向但你只设置1秒时间而第三部分不调用该函数setTimeout只将函数作为回调函数传递给它。请将整个代码写为recaptcha的回调,以便在从服务器验证recaptcha时调用它。

<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"; // Correct here
    }
    document.write("You will be redirected to a new page in 5 seconds ");      
      setTimeout(Redirect,5000); // Correct here
  }
  else alert("You can't proceed!");
</script>

<body>
  <div data-type="image" class="g-recaptcha" data-sitekey="public site key"></div>
</body>
© www.soinside.com 2019 - 2024. All rights reserved.