【发布时间】:2017-05-06 17:43:02
【问题描述】:
我正在尝试使用 google 的功能来检查页面请求是来自人类还是机器人。 通过这个,我想包含一个不可见的验证码,并在页面完成加载后立即提交验证。 我已经浏览了谷歌文档https://developers.google.com/recaptcha/docs/invisible,正常的例子对我来说很好。但是我正在尝试以一种 grecaptcha.execute() 在页面加载后立即触发的方式对其进行调整,我尝试使用 window.onload 或 (window).ready() 并且我得到了相同的错误
grecaptcha is not defined
当然我相信是因为 grecaptcha 脚本没有准备好,实际上如果我设置了 1 秒或 2 秒的超时时间,它会执行的很好。
我尝试将“onload”参数添加到 api.js,但没有成功……顺便说一下,api.js 只是在 HEAD“recaptcha__en.js”中包含了实际的 recaptcha 脚本
我不需要实际的表格或提交任何联系信息,只需要验证查看页面的主题是人还是机器人
任何想法/建议将不胜感激! 谢谢!!
编辑:按要求添加代码:
<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>
【问题讨论】:
-
请分享您迄今为止尝试过的代码并尽可能截图
-
@Jonasw 如果我使用“render=explicit”,我必须使用 render() 方法,这将使 recaptcha 可见,我需要它不可见。
-
@Bucci83 好的,但 onload 应该仍然可以工作,不是吗?
-
@Bucci83 Window.onload = grecaptcha.execute() 将立即执行,您希望 window.onload = ()=>grecaptcha.execute();
-
我把它改成 ()=>grecaptcha.execute() 并且它没有运行......有什么建议吗?你说 window.onload = grecaptcha.execute() 将在解析器到达代码后立即执行,我该如何解决?
标签: javascript invisible-recaptcha