繁体   English   中英

在提交表单之前,如何确保重新验证码js文件已加载?

[英]How to make sure that the re-captcha js file has been loaded before submitting a form?

我有一个验证码和一份注册表。 这是处理程序的简单版本:

  //my_script.js

  document.getElementById("my_form").onsubmit = function(e) {
    e.preventDefault();
    grecaptcha.execute();
    var grecap_resp = grecaptcha.getResponse();
    if (grecap_resp.length !== 0) {

和js文件:

<script src="my_script.js"></script>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>

有时我必须单击两次提交按钮。 我相信这是由于当我单击按钮时尚未加载“ recaptcha / api.js”这一事实造成的。 如我所见,加载完成后,一切运行良好。

我怎样才能解决这个问题?

文档所述 ,您可以传递一个带有函数名称的onload参数,该参数将在加载时被调用:

<script src="my_script.js"></script>
<script src="https://www.google.com/recaptcha/api.js?onload=recaptchaLoaded&render=explicit" async defer></script>

在您的my_script.js

window.recaptchaLoaded = function() { // "window..." so is defined on the global scope
    console.log("Yay! captcha loaded"); // Here you should add your logic to show/enable the button that is disabled/hidden by default.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM