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 src=@string.Format("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);
        });
Marek
  • 3,555
  • 17
  • 74
  • 123
  • Just a guess but I thought `string.Format` was supposed to be like: `@string.Format("

    {0}

    ", serviceDescription[1])`. If not, my bad ...
    – Tim Vermaelen Nov 28 '13 at 19:15
  • What is it? C#, Java or Javascript? – Bart Friederichs Nov 28 '13 at 19:18
  • Cshtml file. Using Javascript in google maps API – Marek Nov 28 '13 at 19:19
  • @TimVermaelen I tried but still got the same Javascript critical error. – Marek Nov 28 '13 at 19:20
  • 1
    How do you know `'` is escaped? I dont see anything like that. What is the Javascript critical error? `'Rejvízská 113 79001 Jeseník'` is a valid JavaScript string. – YK1 Nov 28 '13 at 19:31
  • @YK1 `SCRIPT1015: Unterminated string constant` – Marek Nov 28 '13 at 19:32
  • and my `1` should be an `i` ... too late to edit it – Tim Vermaelen Nov 28 '13 at 19:32
  • if you want render raw string see [HtmlHelper.Raw Method (String)](http://msdn.microsoft.com/en-us/library/gg480740(v=vs.108).aspx) – Grundy Nov 28 '13 at 19:35
  • In JavaScript this could be solved by a function [decodeHtmlEntity](https://gist.github.com/CatTail/4174511) or a jQuery example: `$("
    ").html(s).text();`
    – Tim Vermaelen Nov 28 '13 at 19:36
  • Does this error occur with the example string you've shown? Have you tested with other values whether it works? Error might be on some other line. Could you show us the full loop code? – YK1 Nov 28 '13 at 19:39
  • @YK1 I tried to replace that string and everything worked fine. I really thing that it is escaping my end quotation mark. – Marek Nov 28 '13 at 19:41
  • Only back-slash can escape `'` - which is not there in your sample string. – YK1 Nov 28 '13 at 19:43
  • Maybe just check [this answer](http://stackoverflow.com/questions/4281424/writing-outputting-html-strings-unescaped) – Tim Vermaelen Nov 28 '13 at 19:46
  • @TimVermaelen I tried to edit my code a bit but still same result. May you please check my edited question whether I formated correctly? Thank you for your time. – Marek Nov 28 '13 at 19:58
  • try @Html.Raw(string.Format("

    {0}

    ",serviceDescription[i]))
    – Grundy Nov 28 '13 at 20:16
  • @Grundy still got same error. – Marek Nov 28 '13 at 20:17
  • I feel problem is somewhere else. Please could you show code around the line `"

    "+'@string.Format(serviceDescription[i])'+"

    "`
    – YK1 Nov 28 '13 at 20:24
  • @YK1 I uploaded my code. – Marek Nov 28 '13 at 20:26
  • `serviceDescription[i]` is string? – Grundy Nov 28 '13 at 20:39
  • Yes, it is `list of strings`. – Marek Nov 28 '13 at 20:39
  • Can you check in you final HTML output, what is the values for the line above the one you mentioned? I am suspecting the ` – YK1 Nov 28 '13 at 20:55
  • @YK1 I think is ok because it is formated like `string.Format("data:{0};base64,{1}"` also I'm not sure how to correctly quote the `img src` may you please sent code? Thanks in advance. – Marek Nov 28 '13 at 21:00
  • try `@string.Format("\"data:{0};base64,{1}\""` – YK1 Nov 28 '13 at 21:03
  • @YK1 Still got the same error even after editing the img src and using `"

    "+'@string.Format(serviceDescription[i])'+"

    "`
    – Marek Nov 28 '13 at 21:05
  • difficult to figure. Another thing you can check is whether any of your data value like `@serviceTitle[i].ToString()` could contain a single quote? – YK1 Nov 28 '13 at 21:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42154/discussion-between-marek-and-yk1) – Marek Nov 28 '13 at 21:16

1 Answers1

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]))")
Grundy
  • 13,356
  • 3
  • 35
  • 55