How to hide recaptcha for JavaScript enabled brows

2019-03-01 17:27发布

问题:

I wish to use recaptcha for only non-JavaScript enabled browsers. To do so, I tried using the <noscript> tag as shown below.

<!DOCTYPE html>
<html>
    <head>
        <title>recaptcha</title>
    </head>

    <body>
        <noscript>
            <?php
                require_once('recaptcha-php-1.11/recaptchalib.php');
                echo recaptcha_get_html('xxx');
            ?>
        </noscript>
        <p>Done</p>
    </body>
</html>

When validating the code using http://validator.w3.org, I received the following errors:

Error Line 11, Column 11: The element noscript must not appear as a descendant of the noscript element.

    <noscript>

Error Line 12, Column 147: The frameborder attribute on the iframe element is obsolete. Use CSS instead.

…WAABIZRfA-fKq8eVkFjK_F" height="300" width="500" frameborder="0"></iframe><br/>

Is there anything I can do to remove these errors (at least the first one)? Also, am I better off detecting JavaScript use serverside and just not echoing the captcha?

回答1:

The reCAPTCHA PHP library will be rendering a further < noscript> tag into the DOM inside of your existing one, causing the invalid HTML - because by default it uses javascript.

Take a look here - Displaying reCAPTCHA Without Plugins - Specifically the section "Displaying reCAPTCHA Without Plugins". It shows you an example of what the HTML markup needs to look like. You could try just pasting in the code in the noscript tags...

<iframe src="http://www.google.com/recaptcha/api/noscript?k=your_public_key"
     height="300" width="500" frameborder="0"></iframe><br>
 <textarea name="recaptcha_challenge_field" rows="3" cols="40">
 </textarea>
 <input type="hidden" name="recaptcha_response_field"
     value="manual_challenge">