Too many characters in a character literally

I am adding multiple script using StringBuilder. The script is 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;cb=2244' target='_top'><img src='https://rre.rrt.com/sss.php?zoneid=36540&amp;n=a535c1' border='0' alt='' /></a></noscript>

      

When I add this using the String Builder it gives the "Too many characters in a character literal" error. This is how I do it. 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;cb=2244' target='_top'><img src='https://rre.rrt.com/sss.php?zoneid=36540&amp;n=a535c1' border='0' alt='' /></a></noscript>

");

      

0


a source to share


3 answers


The whole point of use StringBuilder

is to assemble part of a string piece by piece. If you split a long line into logical parts, it will become easier for you to understand any errors and also make changes (and trust me, it becomes much easier to read!).

Also, when using shorthand literals in C #, you still need to avoid 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;cb=2244' target='_top'>");
sb.Append(@"<img src='https://rre.rrt.com/sss.php?zoneid=36540&amp;n=a535c1' border='0' alt='' /></a>");
sb.Append(@"</noscript>");

      

+7


a source


Typically .NET will throw an error when you try to initialize a char with more than one character.

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

Given the heavy use of double and single quotes in your line above, I would recommend splitting it into smaller lines and .append () each separately - there might be a separate .append () response for each line of the line.

Then you should be able to narrow it down and figure out which bit of .NET objects.

Then you can fix the problem by checking for double and single quotes. I can't decipher your application, but it looks like you have to use escaped double quotes in some places.



edit: example of dropping double quotes:

eg

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

      

edit after Cerebru's suggestion:
If you want to use a string literal (with the @ prefix), the double quotes must be escaped by repeating them:

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

      

+3


a source


Alternative answer:

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

You can just put the entire script block in an .aspx page, just like you would with a pure HTML page. Then you don't need to worry about creating it in code.

+1


a source







All Articles