Foreign characters in javascript alerts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ineuw
    New Member
    • May 2006
    • 21

    Foreign characters in javascript alerts

    I must use alerts in French with accented characters and what I get is gibberish.

    How can I display alert messages properly, as HTML character codes for accented characters don't work in javascript alerts?

    The scripts are stored in a .js file and I am referencing it from HTML. The HTML document is 4.01 Transitional and the language is UTF-8.

    Any help is greatly appreciated.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Can you give some examples of codes that don't work.

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by ineuw
      I must use alerts in French with accented characters and what I get is gibberish.

      How can I display alert messages properly, as HTML character codes for accented characters don't work in javascript alerts?

      The scripts are stored in a .js file and I am referencing it from HTML. The HTML document is 4.01 Transitional and the language is UTF-8.

      Any help is greatly appreciated.
      Instead of writing the message in French, write the Unicode for each character.
      You may find the Unicode characters here.

      eg, for alert('ABC');
      use alert('\u0041\u 0042\u0043');

      Comment

      • ineuw
        New Member
        • May 2006
        • 21

        #4
        I apologize but some emergencies prevented me from responding. Thank you both for the replies. The characters in question are with accents or circumflex like é (ANSI 02333) or ô (ANSI 0244) etc.

        Inputting as alert("Veuillez indiquer le nom de societé!") where the alert contains the actual character generates an HTML error when opening the web page.

        Inputting as alert("Veuillez indiquer le nom de societ\0xE9!") using the hex value for the é character, preceded by the escape code, displays a box.

        Your additional comments would be greatly appreciated.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          I have no such problems with the example you have given.

          What's the error message?

          Comment

          • ineuw
            New Member
            • May 2006
            • 21

            #6
            When I open the web page, the error is on line 12. 'Unterminated string constant'. Line 12 is: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

            When I click on "Submit", the error is 'Object expected.

            I changed the charset to ISO-8859-1 and this seems to work for the error message in the external javascript, but then text in the body of the HTML document comes up as gibberish as in:

            'Les données sont acceptées dans n’importe quel format.' The gibberish is the single quote ['] as in [n'importe]

            I also tested the web page with Tidy, and it is error free - (HTML 4.01 Transitional DTD)

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by ineuw
              When I open the web page, the error is on line 12. 'Unterminated string constant'. Line 12 is: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

              When I click on "Submit", the error is 'Object expected.

              I changed the charset to ISO-8859-1 and this seems to work for the error message in the external javascript, but then text in the body of the HTML document comes up as gibberish as in:

              'Les données sont acceptées dans n’importe quel format.' The gibberish is the single quote ['] as in [n'importe]

              I also tested the web page with Tidy, and it is error free - (HTML 4.01 Transitional DTD)
              Meta tag would not give such errors, it must be something in the server side script. Check your quotes. Character set should be UTF-8 only, as you are using Unicode characters.

              Post the HTML part of you code (view source part).

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by ineuw
                When I open the web page, the error is on line 12. 'Unterminated string constant'. Line 12 is: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

                When I click on "Submit", the error is 'Object expected.
                Check the error console of a decent browser. It should show you the line (in the code) which causes the error. It'll be line 12 in the JavaScript.

                Comment

                • ineuw
                  New Member
                  • May 2006
                  • 21

                  #9
                  Thanks to all for the input. I use IE6 for testing because it is still the most prolific browser. I also test the pages with Firefox 2+, Opera 9+ and Safari. The problem is with IE6. Below is the HTML with the javascript for form validation containing the "é" character (Hex 0xE9)(ANSI 0233). IE6 causes an error where the other browsers don't but still don't display the character properly. Just click on Submit, or the Reset buttons.
                  =============== =============== =============== =============== =====

                  [HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/transitional.dt d">
                  <html>
                  <head>
                  <title>Test</title>
                  <meta name="generator " content="HTML Tidy for Windows (vers 6 November 2007), see www.w3.org">
                  <meta name="generator " content="Textpa d version 5.0.3 - http://www.textpad.com ">
                  <meta name="robots" content="noinde x, noarchive">
                  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                  <meta http-equiv="refresh" content="650">

                  <!-- start form validator -->
                  <script type="text/javascript" src="Test.js"></script>
                  </head>
                  <body>

                  <table class="main" width="650" id="policy" align="center" summary="policy ">
                  <tr>
                  <td class="grey" align="center" colspan="2"></td>
                  </tr>
                  <tr>
                  <td>
                  <table border="0" cellpadding="10 " cellspacing="20 " align="center" summary="extern al links">
                  </table>
                  </td>
                  </tr>
                  </table>

                  <br>

                  <form name="Test_form " method="post" action="" enctype="multip art/form-data">
                  <input type="hidden" name="form_user name" value="">
                  <input type="hidden" name="source_do cument" value="Gestion des donnees d'adresse">

                  <br>

                  <table class="main" width="650" align="center" id="quote_reque st" summary="quote_ request">
                  <tr>
                  <td>Soci&eacute ;t&eacute;</td>
                  <td><input class="flat" type="text" name="company" id="company" value="" size="70" maxlength="70"> </td>
                  </tr>
                  <tr>
                  <td>Courriel</td>
                  <td><input class="flat" type="text" name="email_add ress" id="email_addre ss" value="" size="70" maxlength="70">
                  </td>
                  </tr>
                  <tr>
                  <td>Attachez le fichier<br>de donn&eacute;es</td>
                  <td><input class="flat" type="file" name="myform_fi le" size="70" maxlength="100" id="form_file"> </td>
                  </tr>
                  <tr>
                  <td>Quantit&eac ute;<br>approxi mative</td>
                  <td><input class="flat" type="text" name="quantity" id="quantity" value="0" size="10" maxlength="10"> </td>
                  </tr>
                  <tr>
                  <td>Requis par</td>
                  <td><input class="flat" type="text" name="requiredb y" id="requiredby " value="" size="25" maxlength="25"> </td>
                  </tr>
                  </table>
                  <br>
                  <table align="center" id="submit_rese t" summary="submit _reset">
                  <tr>
                  <td align="center"> <input type="reset" id="reset" value="Effacer" onclick="advanc ed_selected(thi s.form)"></td>
                  <td align="center"> <input type="submit" id="submit" value="Soumette z la demande" onclick="return check_form_fiel ds(this.form)"> </td>
                  </tr>
                  </table>
                  </form>
                  <br>
                  <br>
                  <br>
                  </body>
                  </html>[/HTML]
                  =============== =============== =============== =============== =====
                  Test.js
                  =============== =============== =============== =============== =====
                  [CODE=javascript]function check_form_fiel ds(form)
                  {
                  var ValidChars = "0123456789 ";
                  var IsNumber = true;
                  var Char;
                  var i;
                  var today = new Date();
                  if (form.company.v alue == "")
                  {
                  alert("Veuillez indiquer le nom de societé!");
                  form.company.fo cus();
                  return false;
                  }
                  if (form.email_add ress.value == "")
                  {
                  alert("Veuillez indiquer votre adresse courriel!");
                  form.company.fo cus();
                  return false;
                  }
                  if(form.quantit y.value < "1")
                  {
                  alert("Veuillez indiquer la quantite approximative!" );
                  form.quantity.f ocus();
                  return false;
                  }
                  for (i = 0; i < form.quantity.v alue.length && IsNumber == true; i++)
                  {
                  Char = form.quantity.v alue.charAt(i);
                  if (ValidChars.ind exOf(Char) == -1)
                  {
                  IsNumber = false;
                  alert("Veuillez indiquer la quantite approximative!" );
                  form.quantity.f ocus();
                  return false;
                  }
                  }
                  if (form.requiredb y.value == "" || form.requiredby .value == "None")
                  {
                  alert("Veuillez indiquer la date requise!");
                  form.requiredby .focus();
                  return false;
                  }
                  var dt_input = form.requiredby .value;
                  var input_year = dt_input.substr ing(0,4);
                  var input_month = dt_input.substr ing(5,7)-1;
                  var input_day = dt_input.substr ing(8,10);
                  var date_input = new Date(input_year , input_month, input_day);
                  date_input.setH ours(23);
                  if (date_input < today )
                  {
                  alert("La date ne peut pas etre plus tot qu'aujourd'hui! ");
                  form.requiredby .focus();
                  form.requiredby .value = "";
                  return false;
                  }
                  return true;
                  }[/CODE]
                  Last edited by gits; Mar 14 '08, 10:02 AM. Reason: added code tags

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    It should work with ISO-8859-1:
                    [code=html]<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">[/code]

                    PS. please use [code] tags when posting code.

                    Comment

                    • ineuw
                      New Member
                      • May 2006
                      • 21

                      #11
                      Originally posted by acoder
                      It should work with ISO-8859-1:
                      [code=html]<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">[/code]

                      PS. please use [code] tags when posting code.
                      Sorry about the lack of [code] tags. If I change the charset to ISO-8859-1, this eliminates the error of the javascript but introduces another problem in the body od the web page, as follows:

                      The ’ symbol replaces the single quote "n'importe" becomes n’importe.

                      Les données sont acceptées dans n’importe quel format. Nous vous demandons que les fichiers de données nous soient envoyés comprimés, en se servant de n’importe quel logiciel de compression.

                      So, I am stuck no matter which direction I take

                      ty..

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Originally posted by ineuw
                        If I change the charset to ISO-8859-1, this eliminates the error of the javascript but introduces another problem in the body od the web page, as follows:

                        The ’ symbol replaces the single quote "n'importe" becomes n’importe.

                        Les données sont acceptées dans n’importe quel format. Nous vous demandons que les fichiers de données nous soient envoyés comprimés, en se servant de n’importe quel logiciel de compression.
                        I don't see this problem. The sentence appears fine. You didn't have this in the code you posted earlier.

                        Comment

                        • ineuw
                          New Member
                          • May 2006
                          • 21

                          #13
                          Originally posted by acoder
                          I don't see this problem. The sentence appears fine. You didn't have this in the code you posted earlier.

                          UTF-8 = Les données sont acceptées dans n’importe quel format
                          ISO-8859-1 = Les données sont acceptées dans n’importe quel format.

                          When this is displayed in any browser and the charset is ISO-8859-1 then, the single quotes change to these symbols.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Again tested on IE6 + Firefox with no problems. Is this the normal single quote or another character?

                            Can you show a test page or your updated code that demonstrates this problem.

                            Comment

                            • ineuw
                              New Member
                              • May 2006
                              • 21

                              #15
                              Foreign characters in javascript alerts

                              Originally posted by acoder
                              Again tested on IE6 + Firefox with no problems. Is this the normal single quote or another character?
                              Can you show a test page or your updated code that demonstrates this problem.
                              Code:
                              <div>6 colonnes ou moins de données d’adresses afin de satisfaire les exigences</div>
                              The above line is part of a simple French sentence which exists in the actual HTML document. I removed all non-essential portions of the previously posted code so as not to confuse the issue.

                              Please paste code this into anywhere in the previously posted code to see the results.

                              When using iso-8859-1 as the charset, the javascript alerts are corrected but, the French characters in the HTML body turn into symbols as shown here:

                              "6 colonnes ou moins de données d’adresses afin de satisfaire les exigences"
                              Last edited by ineuw; Mar 18 '08, 12:07 AM. Reason: additions and correction to the original

                              Comment

                              Working...