1

I'm trying to insert recaptcha code into a div and it doesn't work. nothing happens. here's the code:

var reCaptcha = '<'+'script type="text/javascript">var RecaptchaOptions = {theme : \'red\',tabindex : 0};<'+'/script>'+'<'+'script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=6LdjvN8SAAAAABUppmhvNOZlq94LauQIGdQ2S042"><'+'/'+'script>'+'<'+'noscript><iframe src="https://www.google.com/recaptcha/api/noscript?k=6LdjvN8SAAAAABUppmhvNOZlq94LauQIGdQ2S042" frameborder="1"></iframe><><textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea><input type="hidden" name="recaptcha_response_field" value="manual_challenge"><'+'/'+'noscript>';

$('#recaptcha').html(reCaptcha);

Any ideas?

5
  • Does the same code work if you put it straight into your HTML (i.e. cut out the JavaScript part)? Apr 11, 2013 at 15:13
  • 1
    You can't include it that way because it uses document.write You must include it directly in the html
    – Kevin B
    Apr 11, 2013 at 15:15
  • 2
    You need to use this way of calling it: developers.google.com/recaptcha/docs/display#AJAX
    – Kevin B
    Apr 11, 2013 at 15:17
  • when you call .html, is the document loaded yet? if not, try put $(function() { and }); around the code Apr 11, 2013 at 15:19
  • Sweet! that Ajax Api worked out great. I wasted so much time on this. Thanks guys!
    – Damien
    Apr 11, 2013 at 15:25

1 Answer 1

2

You can't append a script to an html tag that executes document.write and expect it to work the same way it would if you included it directly in the html. Doing so after the DOM is ready may result in the entire document being replaced with the result of the document.write. In your situation you just need to use the AJAX api rather than the Non-AJAX api.

Recaptcha.create("your_public_key",
  "element_id",
  {
    theme: "red",
    callback: Recaptcha.focus_response_field
  }
);

https://developers.google.com/recaptcha/docs/display#AJAX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.