blaenk / wp-recaptcha

I gave ownership of this project to Google many years ago
Other
38 stars 16 forks source link

attach page-embedded JS to onload event #22

Open lxg opened 14 years ago

lxg commented 14 years ago

If the recaptcha form parts are inserted into the page before the submit button, there is a JS error that "sub" is not defined. This is because of these lines

var sub = document.getElementById('submit');
sub.parentNode.removeChild(sub);

being executed before the element even exists.

The solution would be to use the following, which causes the JS executed when all HTML is loaded:

recaptcha = function()
{
// here comes all the code embedded to the page
};

if (window.attachEvent) { window.attachEvent('onload', recaptcha); }
else if (window.addEventListener) { window.addEventListener('load', recaptcha, false); }
else { document.addEventListener('load', recaptcha, false); }

(The last three lines make sure it runs in all modern browsers.)

blaenk commented 14 years ago

Sorry, I forgot to reply, but I did notice this when you posted it, and it will be added into the next version.