I have this very simple C# function that builds a string that will be written to a html document - the string includes javascript code that will be executed by the browser once the final page is built is sent on its way. I am getting errors - New Line in Constant. I have done this type of thing 1000s of times. I have NOT tried using a StringBuilder class - string should work just as well. I tried escaping the single quotes into quotes (replaced all ' with /") - no luck. What am I missing?
Here is the function:
public string TranslateButtons()
{
string html = "";
html += "<div style='float:right;'>";
html += "<div id='translate-this'><a style='width:180px; height:18px; display:block;' class='translate-this-button' href='http://www.translatecompany.com/'>Translate Company</a></div>";
html += "<script type='text/javascript' src='http://x.translateth.is/translate-this.js'></script>";
html += "<script type='text/javascript'>";
html += "TranslateThis();";
html += "</script>";
html += "</div>";
return html;
}
The offending lines are the ones that include <script> or </script> - I even tried removing lines 1 by 1 - if I include any line with script tags I get the error.
Running: IIS 8.0 - Windows 8 - .NET Framework 4.0
1 Answer 1
I did some experimentation.
If I replace one of the double quotes in your code with a 'Double Acute Accent' I get the new line in constant error.
enter image description here
So you need to make sure all your double quotes are the standard type rather than the 'Double Acute Accent' or 'Double Grave Accent'
They creep in when you copy-paste code from certain places such as rich text editors or websites.
Try deleting your double quotes and re-entering them in your IDE.
Or, try copying the code from your stackoverflow question (as I did) and paste it over 'itself' in your IDE. This may replace the weird double quotes with normal ones.
@symbol?