views:

94

answers:

3

Hello,

In a controller I try to render a javascript google ad to print it into some HTML using JSONP, like this

<script type="text/javascript"><!--
google_ad_client = "pub-23222424";
google_alternate_color = "FFFFFF";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
  src="http://pagead2.google.com/pgad/show_ads.js"&gt;
</script>

So I do:

render :text "var s_response = { content: '<script type=\"text/javascript\"><!--\\ngoogle_ad_client = \"pub-23222424\";\\ngoogle_alternate_color = \"FFFFFF\";\\ngoogle_ad_width = 468;\\ngoogle_ad_height = 60;\\n//--><\\/script>\\n<script type=\"text/javascript\"\\n  src=\"http://pagead2.google.com/pgad/show_ads.js\"&gt;\\n&lt;\\/script&gt;', id: '2'};">

But i get this error:

unterminated string literal

[Break on this error] var response = { content: '<script type="text/javascript"><!-- 

=> That seems like it is a multilines problem right ? But I don't know how to resolve it.

Thanks,

Lucas

A: 

In JavaScript, you cannot spread a string over multiple lines.

In this case, you can leave out those comments so you can put everything in a single line and get rid of the newline characters (\n).

BTW, a valid JSON response would surround every string, whether it represents a key or a value, with double quotes. Also see JSONLint's FAQ:

Proper format for a collection is: { "key": "value" }

In the end, you get:

render :text "var s_response = { \"content\": \"<script type='text/javascript'>google_ad_client = 'pub-23222424';google_alternate_color = 'FFFFFF';google_ad_width = 468;google_ad_height = 60;<\\/script><script type='text/javascript' src='http://pagead2.google.com/pgad/show_ads.js'&gt;&lt;\\/script&gt;\", \"id\": \"2\"};">
Marcel Korpel
A: 

Write huge strings like in my example below and stop escaping them:

render :text => %{ 
    <script type="text/javascript">
    //<!--
    google_ad_client = "pub-23222424";
    google_alternate_color = "FFFFFF";
    google_ad_width = 468;
    google_ad_height = 60;
    //-->
    </script>
    <script type="text/javascript" 
            src="http://pagead2.google.com/pgad/show_ads.js"&gt;
    </script> }
floatless
A: 

Thanks for your responses !

Marcel > The problem is that if I get rid of the newline characters my ad can't display anymore, I tried it "manually". So I may need to replace the "/n" characters before to send the response by something like a unique string and to replace them again after the response is received by the server ?

floatless > Initially I did not escaped my response and it did not work. So I do not think it will change anything, but thanks anyway.

Lucas
You're welcome, but you should post these remarks as comments on our answers, as we will not be notified of these remarks (I only stumbled upon this question again accidentally). Ask the SO team using the ‘contact us’ address to merge your registered and unregistered accounts (providing their numbers: 410029 and 409178). –– Why can't it be displayed? What's the error you get? I checked the string and it just contains valid HTML/JS. Perhaps you should even get rid of the \\ in the end tags, but those shouldn't matter.
Marcel Korpel