0

I have code to show confirm box on clicking on Close Button. And it should show a message along with the name of the month. The month names are in German, and German has special characters for some months like March - März.

The confirm box is not showing the speical characters. Can anyone please help me?

My code:

 function chk_abschliessen()
 {
      var chk = confirm("Möchten Sie diesen Stundenzettel wirklich abschliessen?\nStundenzettel " + '@Model.monat.datum.Value.ToString("MMMM")' + " " + '@Model.monat.datum.Value.Year' + " ");

      if (chk)
      {              
           document.closebutton.submit()                
      }
      else
      {
           return false;
      }
 }

The confirm box shows:

Möchten Sie diesen Stundenzettel wirklich abschliessen? Stundenzettel März 2016

I used a temporary solution, posting it here, as it might help someone else --

function clear_amp(data)
    {
        // for month "März"
        var ret = data.replace("ä", "ä");

        return ret;
    }

    //Abschliessen Messages Check - BEGIN

    function chk_abschliessen()
    {
        var chk = confirm("Möchten Sie diesen Stundenzettel wirklich abschliessen?\nStundenzettel " + clear_amp('@Model.monat.datum.Value.ToString("MMMM")') + " " + '@Model.monat.datum.Value.Year' + " ");
        if (chk)
        { 
          document.closebutton.submit()                
        }
        else
       {
       return false;
        }
  }
SpanB
  • 21
  • 1
  • 5
  • It may also be useful to see what the rendered output is to make sure it hasn't mangled the model values while writing the javascript. – James Thorpe Mar 30 '16 at 09:16
  • The `@Model` in your code indicates that this is a .cshtml Razor page. Do special characters show correctly if you have them in the HTML of the page? Do special characters show correctly if you invoke a `confirm` from a separate JavaScript file? – Richard Ev Mar 30 '16 at 09:22
  • So it's HTML encoding it where it shouldn't be then. I haven't used razor enough to give a definitive answer, but I _think_ `@Html.Raw` may help here. – James Thorpe Mar 30 '16 at 09:42
  • I used a temporary solution, posting it here, as it might help someone else. – SpanB Mar 30 '16 at 11:50
  • Please look to my answer, please mark it if it is suitable for you – Khaled Obaid Mar 30 '16 at 15:49
  • Well, the proposed solution is not flexible enough for different letters, please see my solution below it will be work for any German letter. cheers ! – Khaled Obaid Apr 02 '16 at 12:48

2 Answers2

0

So it should display März instead of März

Actually the JavaScript decodeURIComponent() function is used to decode the characters in the url-specific charactuers.

In your case it is Html encoded, so you can encode/decode the string using HtmlDecode/HtmlEncode functions as following :

HttpUtility.HtmlDecode(@Model.monat.datum.Value.ToString("MMMM"))

Also take a look to this article WebUtility.HtmlDecode vs HttpUtilty.HtmlDecode

This link contains the html characture for the german language

http://character-code.com/german-html-codes.php

Community
  • 1
  • 1
Khaled Obaid
  • 275
  • 3
  • 13
-1

I had same problem with email output and was advised to use encodeURI() or encodeURIComponent() in my code with this issue Foreign letters in Javascript.

Community
  • 1
  • 1
Siobhan Holubicka
  • 147
  • 1
  • 4
  • 17
  • Try running `confirm(encodeURIComponent('ä'))`. URI component encoding is not a suitable solution for encoding here. – James Thorpe Mar 30 '16 at 09:35