I have some forms on the website and Google invisible reCAPTCHA. From time to time something goes wrong and an alert is shown: "Cannot contact reCAPTCHA. Check your connection and try again.". I tried to hide those messages by overriding alert function:
<script>
var _alert = window.alert;
window.alert = function(text) {
if(text.indexOf("reCAPTCHA") === -1){
_alert(text);
}
return true;
};
</script>
However, it doesn't work. Alerts are still shown. This is the code I use to call reCAPTCHA. I use real site key instead of MY_SITE_KEY:
<script>
var widgetNewsletter;
var widgetRegistration;
var captchaCallback = function() {
widgetNewsletter = grecaptcha.render('subscriptionSubmit', {
'sitekey' : 'MY_SITE_KEY',
'callback' : function() {
document.getElementById("newsletter-validate-detail").submit();
}
});
if(document.getElementById("registerFormSubmit") !== null) {
widgetRegistration = grecaptcha.render('registerFormSubmit', {
'sitekey' : 'MY_SITE_KEY',
'callback' : function() {
document.getElementById("form-validate").submit();
}
});
}
};
</script>
<script src='https://www.google.com/recaptcha/api.js?onload=captchaCallback&render=explicit' async="false" defer></script>
How can I stop alerts from appearing?
Reasons why you might get multiple CAPTCHAs:Your Internet Service Provider gave you an IP address that was previously used by hackers within the past few weeks, so our firewall is now mistaking you for a hacker. To fix this, reset your internet router, so you can get a new IP address.
According to the document, you can pass error callback when you call the render()
function. The error callback will be executed when reCAPTCHA encounters an error (usually network connectivity) and cannot continue until connectivity is restored. So your code will look like this
widgetNewsletter = grecaptcha.render('subscriptionSubmit', {
'sitekey' : 'MY_SITE_KEY',
'callback' : function() {
document.getElementById("newsletter-validate-detail").submit();
},
'error-callback': function(){
//show error message
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With