新的Google reCAPTCHA JavaScript名称空间回调 [英] New Google reCAPTCHA JavaScript namespace callback

查看:84
本文介绍了新的Google reCAPTCHA JavaScript名称空间回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试在我们的网站上实施新的Google reCAPTCHA,但是当我们尝试使用命名空间函数从中加载回调时,该回调将不会运行。

We're trying to implement the new Google reCAPTCHA on our website, however when we try and load a callback from it using a namespaced function, the callback does not run.

将回调更改为不使用回调是正确的。我们正在使用与Google Maps API类似的方法,效果很好。

Changing the callback to not use a callback works correctly. We're doing something similar with the Google Maps API, which works fine.

有什么办法可以解决这个问题,或者这是新的Google reCAPTCHA的局限性吗?系统?

Is there any way to get around this, or is this a limitation of the new Google reCAPTCHA system?

代码

<script>
    var namespace = {};
    namespace.captcha = function() {
        alert("Hello world!")
    };
</script>
<script src="//www.google.com/recaptcha/api.js?onload=namespace.captcha&render=explicit" async defer></script>

问题实际上是我们希望使用揭示的模块化模式将所有代码包装在命名空间脚本中。解决此问题的一种方法是创建一个全局变量并将其用作回调,但是它并没有退出我的期望。

The issue really is that we want to keep all our code wrapped up in namespaced scripts using revealing modular pattern. A way around this is to create a global variable and use this as the callback, but it's not quit what I had hoped for.

全局回调

<script>
    var namespace = {};
    namespace.captcha = (function() {         
        function call() {
            alert("Hello world!")
       };
       window.callback = namespace.captcha.call;   
       return call:call;
    })();
</script>
<script src="//www.google.com/recaptcha/api.js?onload=callback&render=explicit" async defer></script>


推荐答案

您可以使用 JavaScript API 来设置回调。

You can do it by using the Javascript API to set the callback.

这将允许您在使用框架时使用命名空间回调,甚至使用受范围保护的回调。

This will allow you to use the namespaced callback, or even the scope protected callback when using a framework.

我无法对其进行测试,例如:

I couldn't test it, so as an example:

var script = document.createElement('script');

script.id = 'container'
script.src = '//www.google.com/recaptcha/api.js?render=explicit';
script.async = true;
script.defer = true;

script.onload = () => { ... };

document.body.appendChild(script);



对于V3



您的 script.onload 函数可能类似于:

grecaptcha.ready(function() {
  namespace.captcha();
});



对于V2



您的 script.onload 函数可能类似于:

grecaptcha.render('container', { 
  callback: namespace.captcha
});

这篇关于新的Google reCAPTCHA JavaScript名称空间回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆