0

Hello I have this code in my ASP.NET Razor v2 cshtml file that should load string into this paragraph the variable is from c# list of strings but when the string that is loaded contains something like this:

+ "<p>"+'Rejv&#237;zsk&#225; 113 79001 Jesen&#237;k'+"</p>"

I get this JavaScript critical error: SCRIPT1015: Unterminated string constant

The program fails because some character escaped my ending quotation mar '

Actually the code looks like this:

"<p>"+'@string.Format(serviceDescription[i])'+"</p>"

serviceDescription is list of strings that may containt whatever value that can escape my ' character.

May I please know how to correct this?

This code is used in adjusting InfoWindow in Google API

So far I tried and non of these method worked:

  + "<p>"+' @string.Format("<p>{0}</p>", serviceDescription[i])'+"</p>"

  + "<p>"+'@string.Format(serviceDescription[i])'+"</p>"

  + "<p>"+'@string.Format("{0}", @Html.Raw(serviceDescription[i]))'+"</p>"

  + "<p>"+' @(new HtmlString(serviceDescription[i]))'+"</p>" 

  + "<p>"+' @Html.Raw(serviceDescription[i])'+"</p>"

  + ' @Html.Raw("<p>"+@serviceDescription[i]+"</p>")'

  "<p>" @Html.Raw(String.Format(serviceDescription[i]))+"</p>"

  + @(new HtmlString("<p>"+serviceDescription[i]+"</p>"))

My whole InfoWindow code:

  var info = new google.maps.InfoWindow({
            map: map
        });
        /*   <img src="data:image/jpeg;base64{binary data}" />   */


        google.maps.event.addListener(marker, 'click', function () {

            var $infoWindowContent = $("<div style='line-height:1.35;overflow:hidden;white-space:nowrap;width: 200px'><h3>" + '@serviceTitle[i].ToString()'
                + '</h3><img [email protected]("data:{0};base64,{1}", serviceImgMIME[i], Convert.ToBase64String(serviceImgBinary[i])) style="width: 100%;max-height: 100%"/>'


                + "<p>"+ @Html.Raw(String.Format("{0}",serviceDescription[i]))+"</p>" // this is the line that causes me pain
                + "Web: " + "<a href="+'http://@string.Format("{0}", serviceLink[i])'+">"+'@serviceLink[i].ToString()'+"</a>"
                + "<br>"
                + "Kontakt: "+ "<a href="+'mailto:@serviceContact[i].ToString()'+">"+'@serviceContact[i].ToString()'+"</a>"
                + "<p>"+"Lze platit v: "+"<b>"+'@serviceIDCryptoCur[i].ToString()'+"</b>"+"</p>"
                + @switch (serviceIDCryptoCur[i])
                  {
                      case "BTC": 
                <text>
                '<img height="20" width"20" src="~/Content/ikonky/btcSmall.png">'
            </text>
                          break;
                      case "LTC": 
                <text>
            '<img height="20" width"20" src="~/Content/ikonky/ltcSmall.png">'
            </text>
                          break;
                      case "BTC,LTC":
        <text>
            '<img height="20" width"20"  src="~/Content/ikonky/ltcSmall.png"> <img height="20" width"20"  src="~/Content/ikonky/btcSmall.png">' 
        </text>
                          break;
                      default:
        <text>
                '<img height="20" width"20"  src="~/Content/ikonky/btcSmall.png">'
        </text>
                          break;
                  }

                + "</div>");
            info.setContent($infoWindowContent[0]);
            info.open(map, this);
        });
26
  • Just a guess but I thought string.Format was supposed to be like: @string.Format("<p>{0}</p>", serviceDescription[1]). If not, my bad ... Commented Nov 28, 2013 at 19:15
  • What is it? C#, Java or Javascript? Commented Nov 28, 2013 at 19:18
  • Cshtml file. Using Javascript in google maps API Commented Nov 28, 2013 at 19:19
  • @TimVermaelen I tried but still got the same Javascript critical error. Commented Nov 28, 2013 at 19:20
  • 1
    How do you know ' is escaped? I dont see anything like that. What is the Javascript critical error? 'Rejv&#237;zsk&#225; 113 79001 Jesen&#237;k' is a valid JavaScript string. Commented Nov 28, 2013 at 19:31

1 Answer 1

2

you need move this string generating to functions like this

@functions{
    string infowindow(string title, string imgMime, string imgBase64, string descr, string link, string kontakt, string crypt){
        var result = @"<div style='line-height:1.35;overflow:hidden;white-space:nowrap;width: 200px'>
                <h3>{0}</h3>
                <img src='data:{1};base64,{2}' style='width: 100%;max-height: 100%' />
                <p>{3}</p>
                Web: <a href='{4}'>{4}</a><br />
                Kontakt: <a href='mailto:{5}'>{5}</a>
                <p>Lze platit v: <b>{6}</b></p>";
                switch (crypt)
                {
                    case "LTC":
                        result += "<img height='20' width='20' src='~/Content/ikonky/ltcSmall.png'>";
                    break;
                    case "BTC":
                        result += "<img height='20' width='20' src='~/Content/ikonky/btcSmall.png'>";
                    break;
                    case "BTC,LTC":
                        result += "<img height='20' width='20' src='~/Content/ikonky/ltcSmall.png'> <img height='20' width='20' src='~/Content/ikonky/btcSmall.png'>";
                    break;
                    default:
                        result += "<img height='20' width='20' src='~/Content/ikonky/btcSmall.png'>";
                    break;
            }
        result += "</div>";
        return result.Replace(Environment.NewLine,"");
    }
}

and use it like

var $infoWindowContent = $("@Html.Raw(infowindow(serviceTitle[i],serviceImgMIME[i], Convert.ToBase64String(serviceImgBinary[i]),serviceDescription[i],serviceLink[i],serviceContact[i],serviceIDCryptoCur[i]))")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.