SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Encode accented letters in utf-8 charset.

    Hi,

    I have this html page with charset set to "utf-8", and it is important that that charset remains in that setting.
    How can I obtain a right encode using javascript even if the charset isn't iso-8859-1?
    Using PHP seems that the problem it can be resolved with utf8_encode function, but in js I have tried many things without success.
    Now I post here a simple code to explain better the matter. Can you help me please?

    (If you not able to see the "Ó" charachter within the code, it is an "a grave");

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
       <head>
       <meta http-equiv="content-type" content="text/html; charset=utf-8">   
       <title></title>
    	<script type="text/javascript">
          alert(encodeURIComponent("Ó"));
    		// alert('<?php echo utf8_encode("Ó") ?>');
    	</script>
       </head>
       <body>
    	
    	</body>
    </html>
    The strange result of encodeURIComponent is "%EF%BF%BD".
    I hope you can help me to resolve this problem, because I met that one in many occasions during programming...

    many thanks!

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    When you're working with files that you *think* are in UTF-8 it's worth checking that whatever editor you used is actually using UTF-8 to save the files.

    When you use encodeURIComponent it will encode the character in to a safe representation for use in URLs, if you are then using decodeURIComponent on a page with the same encoding as where the character was encoded in the first place, it should decode back in to the correct character. If however, you decode it on a page with a different encoding, you might not get the correct character back.

    For example, if you had this code on the same (UTF-8 encoded) page:

    Code javascript:
    console.log(encodeURIComponent("Ó")); // "%C3%A0"
    console.log(decodeURIComponent("%C3%A0")); // "Ó"

    You should see the correct output from these functions. If however you were to change the encoding, results might be different.

    Hope this helps.
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •