WordPress.org

Ready to get started?Download WordPress

Forums

WP-reCAPTCHA
JavaScript error on pages that don't have submit button (21 posts)

  1. llewellynworld
    Member
    Posted 3 years ago #

    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!

  2. KZeni
    Member
    Posted 3 years ago #

    I approve of this message, and would love to see that implemented considering it really is a quick fix.

  3. Claude DiDomenica
    Member
    Posted 3 years ago #

    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!

  4. llewellynworld
    Member
    Posted 3 years ago #

    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?

  5. Claude DiDomenica
    Member
    Posted 3 years ago #

    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!

  6. llewellynworld
    Member
    Posted 3 years ago #

    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.

  7. Claude DiDomenica
    Member
    Posted 3 years ago #

    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.

  8. Jonathan
    Member
    Posted 3 years ago #

    @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?

  9. Frankie Jarrett
    Member
    Posted 3 years ago #

    @llewellynworld thanks, exactly what i needed too

    //Frankie

  10. llewellynworld
    Member
    Posted 3 years ago #

    @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.

  11. Jonathan
    Member
    Posted 3 years ago #

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

  12. Calophi
    Member
    Posted 3 years ago #

    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.

  13. brchok
    Member
    Posted 3 years ago #

    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.

  14. Rhand
    Member
    Posted 2 years ago #

    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!

  15. Jonathan
    Member
    Posted 2 years ago #

    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>
  16. lcool
    Member
    Posted 2 years ago #

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

  17. KZeni
    Member
    Posted 2 years ago #

    lcool,

    3.1.4 just came out... it still has this issue. I don't know how that's possible. I updated to 3.1.4 & re-instated this patch.

  18. Rhand
    Member
    Posted 2 years ago #

    Really? Haven't updated yet. well, hopefully the developer reads this.

  19. Jake Sully
    Member
    Posted 2 years ago #

    Until now the issue persists.

    'document.getElementById(...)' is null or not an object

    I hope this plugin isn't abandoned. Thanks for sharing the fix, you guys saved me a lot of debugging time. :)

  20. OC2PS
    Member
    Posted 2 years ago #

    No updates since April 2011.

    No replies from author on these forums in months.

    Might well be abandoned.

  21. Jake Sully
    Member
    Posted 2 years ago #

    Has anyone updated the plugin and saw that the error is fixed? I'm cautious updating the plugin, it might undo the patch.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic