google / recaptcha

PHP client library for reCAPTCHA, a free service to protect your website from spam and abuse.
http://www.google.com/recaptcha/
BSD 3-Clause "New" or "Revised" License
3.48k stars 771 forks source link

Official "Loading reCAPTCHA asynchronously" does not work #485

Open Benjaminhu opened 2 years ago

Benjaminhu commented 2 years ago

Official "Loading reCAPTCHA asynchronously" does not work:

Uncaught TypeError: grecaptcha.render is not a function

https://jsfiddle.net/657b1gso/

The problem:

...
  grecaptcha.ready = function(cb){
    if(typeof grecaptcha === 'undefined') {  // <- never be undefined
...
ryan-carbon commented 2 years ago

I am also experiencing this issue with the code sample

soundslocke commented 1 year ago

We ran into this issue as well: the code doesn't work as published!

It's strange, I'm not sure what situation or problem they may have wanted to solve or address via that logic, but it isn't correct as-is. I wonder how many sites around the internet are broken in a subtle way because of that snippet in the docs? :thinking:

Here's the implementation we are using to accomplish this:

if (typeof grecaptcha === 'undefined') {
    grecaptcha = {
        ready: callback => {
            const c = '___grecaptcha_cfg';
            window[c] = window[c] || {};
            (window[c].fns = window[c].fns || []).push(callback);
        }
    };
}