admin管理员组文章数量:1421256
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.
Changing the callback to not use a callback works correctly. We're doing something similar with the Google Maps API, which works fine.
Is there any way to get around this, or is this a limitation of the new Google reCAPTCHA system?
Code
<script>
var namespace = {};
namespace.captcha = function() {
alert("Hello world!")
};
</script>
<script src="//www.google/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.
Global callback
<script>
var namespace = {};
namespace.captcha = (function() {
function call() {
alert("Hello world!")
};
window.callback = namespace.captcha.call;
return call:call;
})();
</script>
<script src="//www.google/recaptcha/api.js?onload=callback&render=explicit" async defer></script>
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.
Changing the callback to not use a callback works correctly. We're doing something similar with the Google Maps API, which works fine.
Is there any way to get around this, or is this a limitation of the new Google reCAPTCHA system?
Code
<script>
var namespace = {};
namespace.captcha = function() {
alert("Hello world!")
};
</script>
<script src="//www.google./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.
Global callback
<script>
var namespace = {};
namespace.captcha = (function() {
function call() {
alert("Hello world!")
};
window.callback = namespace.captcha.call;
return call:call;
})();
</script>
<script src="//www.google./recaptcha/api.js?onload=callback&render=explicit" async defer></script>
Share
Improve this question
edited Jun 25, 2018 at 13:40
user7637745
9852 gold badges14 silver badges27 bronze badges
asked Jan 5, 2015 at 9:51
TobyToby
1,6712 gold badges19 silver badges31 bronze badges
6
- Yes, it looks like they don't allow it. – Emmanuel Delay Commented Jan 5, 2015 at 12:46
- "Changing the callback to not use a callback works correctly" Did you mean to not use a namespace? – xr280xr Commented Aug 16, 2016 at 20:32
- I'm having same problem PLUS the issue that we minify function names, so our global namespace function names are unpredictable. – webdevinci Commented Oct 14, 2016 at 13:33
- What is the problem with using <script src="//www.google./recaptcha/api.js?onload=namespace.captcha.call&render=explicit" async defer></script> – Roumelis George Commented Oct 25, 2016 at 11:56
- 1 I also can't get recaptcha V2 to work with a namespace'd module function as a callback. – Gurnzbot Commented Jul 7, 2017 at 13:36
2 Answers
Reset to default 2You 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./recaptcha/api.js?render=explicit';
script.async = true;
script.defer = true;
script.onload = () => { ... };
document.body.appendChild(script);
For the V3
Your script.onload
function could be like:
grecaptcha.ready(function() {
namespace.captcha();
});
For the V2
Your script.onload
function could be like:
grecaptcha.render('container', {
callback: namespace.captcha
});
The file api.js load another stuff.
The parameter onload=functionCallback in:
//www.google./recaptcha/api.js?onload=callback&render=explicit
is used to determinate the function that will be loaded when the stuff in the api.js is loaded.
本文标签: New Google reCAPTCHA JavaScript namespace callbackStack Overflow
版权声明:本文标题:New Google reCAPTCHA JavaScript namespace callback - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745319595a2653315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论