• The function “save_comment_script” needs to be modified in the recaptcha.php page to check for the existence of the id ‘submit’. Otherwise, the getElementById(‘submit’) returns null and the function throws a JavaScript error on the page. I’m using IE8, WordPress 3.1.2, wp-recaptcha version 3.1.3.

    Works fine on my comment page where this button exists, but throws the error on any page that does not show the comment form with reCaptcha (and that does not include a ‘submit’ id). Wrapping this code around the lines in the function works for me:

    if(document.getElementById(‘submit’) != null)

    It was easy enough to manually fix in the plugin code, but it would be great if that check could be added to the main plugin source so that I don’t have to keep remembering to add it in every time I upgrade the plugin.

    Thanks!

Viewing 15 replies - 1 through 15 (of 20 total)
  • I approve of this message, and would love to see that implemented considering it really is a quick fix.

    Hello llewellynworld. I’m having the same problem. I tried to patch the script (wp-recaptcha/recaptcha.php) and although I got the JS error to disappear, I’m now getting HTML validation errors. Looks like the <\body> tag is getting snipped off somehow now.

    It could be I did the patch wrong. It would be helpful if you could post the code to do the patch, in-context. I was not sure how to wrap the code around the function as you suggested.

    Thanks!

    Thread Starter llewellynworld

    (@llewellynworld)

    Sure thing. At line 403 in the recaptcha.php page, there’s a function called “save_comment_script”. In the middle of it is a block of code that currently looks like this:

    <script type="text/javascript">
    var sub = document.getElementById('submit');
    document.getElementById('recaptcha-submit-btn-area').appendChild (sub);
    document.getElementById('submit').tabIndex = 6;
    if ( typeof _recaptcha_wordpress_savedcomment != 'undefined') {
    	document.getElementById('comment').value = _recaptcha_wordpress_savedcomment;
    }
    document.getElementById('recaptcha_table').style.direction = 'ltr';
    </script>

    Change that to look like this instead:

    <script type="text/javascript">
    if(document.getElementById('submit') != null)
    {
    	var sub = document.getElementById('submit');
    	document.getElementById('recaptcha-submit-btn-area').appendChild (sub);
    	document.getElementById('submit').tabIndex = 6;
    	if ( typeof _recaptcha_wordpress_savedcomment != 'undefined') {
    		document.getElementById('comment').value = _recaptcha_wordpress_savedcomment;
    	}
    	document.getElementById('recaptcha_table').style.direction = 'ltr';
    }
    </script>

    I basically wrapped an if statement around the chunk of code so that it makes sure the submit Id actually exists before it attempts to do anything with it. Does that help?

    Hey llewellynworld, you’re a pal. Worked like a charm!

    I now see what you were telling this knucklehead what to do. And it’s useful to know for other stuff that might come up related to functions and/or null issues…

    If it’s OK with you, I’d like to post this fix on my blog, with attribution of course. If you want, I can link to here or somewhere else. Contact me via my blog if you want the somewhere else, or post the link here. From time to time, I post blogmaster tips and this is a good one since so I assume lots of folks use the plug-in.

    I’ll also add one or two other tips on getting the plug-in to validate (JS on IE and validator.w3.org) that I noticed. I need to check my notes, but I think both aren’t issues in the code, per se.

    Thanks again!

    Thread Starter llewellynworld

    (@llewellynworld)

    Aaahh, no meed for attribution. I didn’t think to check the validation. We have other things that cause the page to not validate, so I didn’t look to see if this plugin made it worse or not.

    OK cool. I’ll just link to here as the source.

    I’m pretty good at getting stuff to validate (or finding an alternative that does validate), hit me up any time and I can at least take a peek.

    @llewellynworld, thanks a lot for the fix. Is there a way to update plugins like this without losing the ability to update them in the future?

    @llewellynworld thanks, exactly what i needed too

    //Frankie

    Thread Starter llewellynworld

    (@llewellynworld)

    @jonathan, I haven’t really spent time digging into it, so I’m not sure if there is or not. I don’t spend a lot of time on our WordPress install as the rest of our sites takes up all of my time.

    @llewellynworld, not a problem. Thanks anyways for the response.

    This is exactly what I needed. Looks like the developers of this plugin don’t test properly in IE, because even 9 was having issues.

    I get the same error on every browser, not just IE.
    Thank you for posting the fix!
    It’s a pity that the plugin is not updated…

    However I had to change a little bit your fix:

    <script type="text/javascript">
                if(document.getElementById('recaptcha-submit-btn-area') != null)
                {
                        var sub = document.getElementById('submit');
                        document.getElementById('recaptcha-submit-btn-area').appendChild (sub);
                        document.getElementById('submit').tabIndex = 6;
                        if ( typeof _recaptcha_wordpress_savedcomment != 'undefined') {
                                document.getElementById('comment').value = _recaptcha_wordpress_savedcomment;
                        }
                        document.getElementById('recaptcha_table').style.direction = 'ltr';
                }
                </script>

    Basically I’m checking if the captcha exist instead of checking if the submit button exist. Since in my website the captcha is disabled for logged-in users even if the comment form is present, so they were still receiving the error.

    I have this error as well. Would be better to get a fix done by the plugin developer then a fix done like this. But that will probably take some time to implement.
    @ llewellynworld Thanks for the fix!

    Hi,

    I was still getting the error in both IE6 and IE7, but putting together the if statements seemed to fix the problem. The code is now:

    <script type="text/javascript">
                    if(document.getElementById('submit') != null && document.getElementById('recaptcha-submit-btn-area') != null)
                    {
                      var sub = document.getElementById('submit');
                      document.getElementById('recaptcha-submit-btn-area').appendChild (sub);
                      document.getElementById('submit').tabIndex = 6;
                      if ( typeof _recaptcha_wordpress_savedcomment != 'undefined') {
                              document.getElementById('comment').value = _recaptcha_wordpress_savedcomment;
                      }
                      document.getElementById('recaptcha_table').style.direction = 'ltr';
                    }
    </script>

    Thanks, that worked perfectly! Any chance this will get added to the plugin officially?

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘[Plugin: WP-reCAPTCHA] JavaScript error on pages that don't have submit button’ is closed to new replies.