tags:

views:

3091

answers:

3

I am adding some script using the StringBuilder. The script is as shown below.

<script type='text/javascript'><!--    //<![CDATA[
    var m3_u = (location.protocol == 'https:' ? 'https://rre.rrt.com/sss.php' : 'https://rre.rrt.com/sss.php');
    var m3_r = Math.floor(Math.random() * 99965449);
    if (!document.MAX_used) document.MAX_used = ',';
    document.write("<scr" + "ipt type='text/javascript' src='" + m3_u);
    document.write("?zoneid=311120&amp;target=_top");
    document.write('&amp;cb=' + m3_r);
    if (document.MAX_used != ',') document.write("&amp;exclude=" + document.MAX_used);
    document.write(document.charset ? '&amp;charset=' + document.charset : (document.characterSet ? '&amp;charset=' + document.characterSet : ''));
    document.write("&amp;loc=" + escape(window.location));
    if (document.referrer) document.write("&amp;referer=" + escape(document.referrer));
    if (document.context) document.write("&context=" + escape(document.context));
    if (document.mmm_fo) document.write("&amp;mmm_fo=1");
    document.write("'><\/scr" + "ipt>");
//]]>-->
</script><noscript><a href='https://rre.rrt.com/sss.php?n=ad4565c1&amp;amp;cb=2244' target='_top'><img src='https://rre.rrt.com/sss.php?zoneid=36540&amp;amp;n=a535c1' border='0' alt='' /></a></noscript>

When I add this using String Builder, it gives error "Too many characters in character literal" This is how I am doing. How to do it?

sb.append(@"<script type='text/javascript'><!--    //<![CDATA[
    var m3_u = (location.protocol == 'https:' ? 'https://rre.rrt.com/sss.php' : 'https://rre.rrt.com/sss.php');
    var m3_r = Math.floor(Math.random() * 99965449);
    if (!document.MAX_used) document.MAX_used = ',';
    document.write("<scr" + "ipt type='text/javascript' src='" + m3_u);
    document.write("?zoneid=311120&amp;target=_top");
    document.write('&amp;cb=' + m3_r);
    if (document.MAX_used != ',') document.write("&amp;exclude=" + document.MAX_used);
    document.write(document.charset ? '&amp;charset=' + document.charset : (document.characterSet ? '&amp;charset=' + document.characterSet : ''));
    document.write("&amp;loc=" + escape(window.location));
    if (document.referrer) document.write("&amp;referer=" + escape(document.referrer));
    if (document.context) document.write("&context=" + escape(document.context));
    if (document.mmm_fo) document.write("&amp;mmm_fo=1");
    document.write("'><\/scr" + "ipt>");
//]]>-->
</script><noscript><a href='https://rre.rrt.com/sss.php?n=ad4565c1&amp;amp;cb=2244' target='_top'><img src='https://rre.rrt.com/sss.php?zoneid=36540&amp;amp;n=a535c1' border='0' alt='' /></a></noscript>

");
+2  A: 

.NET usually gives the error when you are trying to initalise a char type with more than one character.

From the conversation here it seems you may also get the error when .NET gets confused by single quotes and assumes you are trying to use them to define a 'char' literal.

Given the heavy use of double and single quotes in your string above, I'd recommend splitting it up into smaller strings and .append() each one separately - maybe a seperate .append() call for each line of the string.

Then you will be able to narrow it down and find out which bit .NET is objecting to.

You should then be able to fix the problem by checking through the double and single quotes. I can't really decipher your append but it looks like you should be using escaped double quotes \" in some places.

edit: example of escaping double quotes:

e.g

sb.Append("document.write(\"<scr\" + \"ipt type='text/javascript' src='\" + m3_u);");

edit after Cerebrus suggestion:
If you want to use a verbatim string literal (with the @ prefix) then the double-quotes must be escaped by repeating them:

sb.Append(@"document.write(""<scr"" + ""ipt type='text/javascript' src='"" + m3_u);");
codeulike
I did that..I broke into smaller strings, but the error did not go. Hence I asked for here.The error starts from this line document.write("<scr" + "ipt type='text/javascript' src='" + m3_u);
KJai
... probably because thats the first " quote in your string. See edit above.
codeulike
@Codeulike: The OP has used verbatim literal strings (indicted by the @ character). Therefore, quotes will be escaped with another quote. Please see my answer. However, +1 for recommending that the OP break down his/her string into smaller parts.
Cerebrus
Yes I had spotted he was using the @-verbatim but he wasn't escaping quotes using either method so I kindof ignored it and didn't bother with the @ in my answer : ) ... I'll stick another example in for completeness ...
codeulike
Thanks Codeulike!
KJai
+6  A: 

The whole point of using a StringBuilder is to build the string part by part. If you break up the long string into logical portions, then it becomes easier for you to figure out any errors as well as make modifications (and believe me, it becomes much easier to read!).

Also, When using verbatim string literals in C#, you still need to escape double quotes with another double quote. The following code compiles correctly:

sb.Append(@"<script type='text/javascript'>");
sb.Append(@"<!--    //<![CDATA[");
sb.Append(@"var m3_u = (location.protocol == 'https:' ? 'https://rre.rrt.com/sss.php' : 'https://rre.rrt.com/sss.php');");
sb.Append(@"var m3_r = Math.floor(Math.random() * 99965449);");
sb.Append(@"if (!document.MAX_used) document.MAX_used = ',';"");");
sb.Append(@"document.write(""<script type='text/javascript' src='"" + m3_u);");
sb.Append(@"document.write(""?zoneid=311120&amp;target=_top"");");
sb.Append(@"document.write('&amp;cb=' + m3_r);");
sb.Append(@"if (document.MAX_used != ',') document.write(""&amp;exclude="" + document.MAX_used);");
sb.Append(@"document.write(document.charset ? '&amp;charset=' + document.charset : ");
sb.Append(@"(document.characterSet ? '&amp;charset=' + document.characterSet : ''));");
sb.Append(@"document.write(""&amp;loc="" + escape(window.location));");
sb.Append(@"if (document.referrer) document.write(""&amp;referer="" + escape(document.referrer));");
sb.Append(@"if (document.context) document.write(""&context="" + escape(document.context));");
sb.Append(@"if (document.mmm_fo) document.write(""&amp;mmm_fo=1"");");
sb.Append(@"document.write(""'><\/script>"");");
sb.Append(@"//]]>-->");
sb.Append(@"</script>");
sb.Append(@"<noscript>");
sb.Append(@"<a href='https://rre.rrt.com/sss.php?n=ad4565c1&amp;amp;cb=2244' target='_top'>");
sb.Append(@"<img src='https://rre.rrt.com/sss.php?zoneid=36540&amp;amp;n=a535c1' border='0' alt='' /></a>");
sb.Append(@"</noscript>");
Cerebrus
+1 cos your answer is better than mine because you actually re-wrote the whole shebang for him : )
codeulike
Lol, thanks. Yeah, it was like swimming in spaghetti! :P
Cerebrus
Thanks so much Cerebus for explaining it so well..I don't think I will ever forget that, at least till I am a programmer ;)
KJai
Hey, you're welcome. It was my pleasure and thanks for accepting the answer! :-)
Cerebrus
+1  A: 

An alternative answer:

As your javascript code is static code anyway (its the same every time, no ASP.NET variables are combined into it), why bother to build it in ASP.NET code?

You could just put the whole script block into the .aspx page, just like you would with a pure HTML page. Then you don't need to bother building it in code.

codeulike
my thoughts exactly
Greg B
Interesting! Unless the OP's sample is just that - a sample. ;-)
Cerebrus