1

I need to store the ReCaptcha code in a JavaScript variable and then dynamically show the captcha (in a dynamically created DIV). The problem is the following code cannot be stored in a variable:

<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k="></script><noscript><iframe src="http://www.google.com/recaptcha/api/noscript?k=" 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"/></noscript>

I get the error in Firebug: unterminated string literal. How to make it work?

Horos
  • 11
  • 2
  • The reason Firebug thinks the string's unterminated (as should any standards-compliant browser) is because (1) you're using quotation marks inside the string, which need to be escaped with backslashes (exactly as @Rob mentioned), and (2) you can't include HTML tags inside JavaScript literals. @Horos's answer has what you're looking for to get around this (which is why I didn't post an answer of my own). – Michael Hoffmann Apr 25 '11 at 01:44

2 Answers2

1

You are probably storing it in a string using " as the delimiters, which is clashing with the " used on the attributes' value delimiters.

Try using single quote ('), or escape each " with a backslash (\") to have it treated literally.

alex
  • 479,566
  • 201
  • 878
  • 984
1
var s = '<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k="></script><noscript><iframe src="http://www.google.com/recaptcha/api/noscript?k=" 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"/></noscript>'
wong2
  • 34,358
  • 48
  • 134
  • 179