从外部JS文件reCaptcha onload函数?

时间:2017-08-28 21:56:22

标签: javascript recaptcha

我正在尝试将reCaptcha实现到我的表单中,然后在前端进行验证后从提交按钮中删除disabled状态。问题是我需要在验证reCaptcha后从外部Javascript文件访问一些变量,所以我需要加载函数在外部Javascript文件中。当然,这不起作用,因为加载函数超出范围,因此我得到错误ReCAPTCHA couldn't find user-provided function: loadCaptcha

这是我的HTML代码:

<html>
<head>
    <script src='https://www.google.com/recaptcha/api.js?onload=loadCaptcha&render=explicit'></script>
</head>
<body>
    <form>
        ....
        <div id="captcha-container"></div>
    </form>
</body>

这是我的Javascript(来自外部javascript文件):

var captchaContainer = null;
var loadCaptcha = function() {
    captchaContainer = grecaptcha.render('captcha-container', {
        'sitekey' : 'xxxxxxx',
        'callback' : function(response) {
            alert(response);
        }
    });
};

如何设置它以便在加载api Javascript文件时呈现reCaptcha,但是在我的外部Javascript文件中执行它,以便我可以在loadCaptcha的回调中访问那里的一些变量功能

1 个答案:

答案 0 :(得分:-1)

我认为你可以这样做:

window.loadCaptcha = () => {/*do something*/}