Boris
Boris

Reputation: 10234

Handle special characters in JavaScript

I have a web page with the following code snippet:

...
<td nowrap="true" valign="top" width="190px" class="ms-formlabel">
   <h3 class="ms-standardheader">
      <nobr>
         QA Groupe des papiers
         <span class="ms-formvalidation" title="Ce champ est obligatoire." > *</span>
      </nobr>
   </h3>
</td>
...
<td nowrap="true" valign="top" width="190px" class="ms-formlabel">
   <h3 class="ms-standardheader">
      <nobr>
         QA Description métier
         <span class="ms-formvalidation" title="Ce champ est obligatoire." > *</span>
      </nobr>
   </h3>
</td>
<td valign="top" class="ms-formbody">
...

I am developing a JavaScript which is supposed to hide the table rows containing the strings QA Groupe des papiers and QA Description métier.

This is the script:

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function($) {
       $("h3.ms-standardheader:contains('QA Groupe des papiers')").closest("tr").hide();
       $("h3.ms-standardheader:contains('QA Description métier')").closest("tr").hide();
    });
</script>

The script is working fine for the first row, but not for the second. I know the problem is related to the special character é in the second script, but I don't know how to resolve the problem. Please help, thanks.

Upvotes: 0

Views: 1971

Answers (2)

user2428118
user2428118

Reputation: 8104

Unicode is a complex thing. The character set has two different was of writing characters such as á, é etc.

Replacing one of the é characters with the other might solve your problem. (You can read more about this at Wikipedia.)

If you continue to have problems, also check the encoding using which the JavaScript file and the web page are encoded.

Both the web page and the JavaScript file should be UTF-8 encoded and communicate this to the browser. This means that the web page must have at least one of the following

  • An HTTP header which specifies the character encoding; e.g. Content-Type: text/html; charset=UTF-8
  • A meta tag which specifies the encoding; <meta charset=UTF-8> (HTML5) or <meta http-equiv=Content-Type content='text/html; charset=UTF-8>
  • A Byte Order Mark (not recommended)

The JavaScript file can be indicated as UTF-8 using one or more of the following methods:

  • Using the charset attribute
  • Using an HTTP header which specifies the character encoding; e.g. Content-Type: text/javascript; charset=UTF-8

Further reading:

Upvotes: 1

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201518

The probable explanation is that the character encoding of the JavaScript file has not been declared properly in HTTP headers. Probably the JavaScript file is interpreted as ISO-8859-1 or Windows-1252 encoded but is actually UTF-8 encoded, or vice versa, which means that “é” isn’t what it should be. A simple way to check this out is alert('é'). Regarding encodings issues, check out the W3C page Character encodings.

Upvotes: 0

Related Questions