Link to home
Start Free TrialLog in
Avatar of ussher
ussherFlag for Japan

asked on

javascript change character encoding

Hi,

I want to have a selection option on my website that allows the user to change the character encoding of the page in the same way as would happen if they changed the settings in their browser.

example. in firefox to change the encoding of a page i select VIEW->CHARACTER ENCODING-> UTF-8

I would like to have this as an option on my website
* utf-8
*euc-jp
*shift-jis.

The reason for this is that the site will be recieving data in theses formats and when the user views the page some of the characters will not be visible until the character encoding is changed.  

Since the data displayed will come from a post form it would be much more convieninet to use the browser to change the encoding and better to have a button than trying to teach the users how to use thier borwsers.

I dont want to have to re-retrieve the data from the database whenever anybody changes their encoding.
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Why not code everything in UTF-8 and be done with it?

Otherwise if possible, you need to change the meta tag - not even sure if that will do it.
Does not seem writeable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <title></title>
  <script>
  var saveCharset = document.getElementsByTagName('meta')[0].content.split('charset=')[1];
  var charSets = new Array(
  "windows-1250",
  "iso-8859-1",
  "UTF-8"
  );
  function changeMeta(idx) {
    var met = document.getElementsByTagName('meta');
    var newSet = met[0].content.replace(saveCharset,charSets[idx]);
    saveCharset=charSets[idx];
    met[0].content=newSet;
    alert('Changed to '+document.getElementsByTagName('meta')[0].content.split('charset=')[1])
//    document.body.innerHTML=document.body.innerHTML;
    return false;
  }
  </script>
  </head>
  <body >
 
  <a href="#" onClick="return changeMeta(0)">1250</a><br>
  <a href="#" onClick="return changeMeta(1)">8859-1</a><br>
  <a href="#" onClick="return changeMeta(2)">UTF-8</a><br>
  éàéöä

  </body>
</html>
Avatar of ussher

ASKER

Hi mplungjan,

the interface is in utf but the problem is with the incoming data.  It is a web advertisment tracking script.

So if the site that the link is coming from is in shift_jis or euc-jp then when the link is clicked on it sends data to the tracking script in the encoding that the page was.

So when i try to display the link in my site which is encoded in utf8 then the id tag gets screwed up.

hxxp://mysite.com/ad_tracking_script.php?tracking_id=something_in_japanese.

The 'something_in_japanese' is identifying data for this ad.
something_in_japanese // placed on a UTF8 site goes into the database clear.
something_in_japanese // placed on a EUC-JP site goes into the database messed up.
something_in_japanese // placed on a SHIFT-JIS site goes into the database messed up.

so when i view the reports i get output like

something_in_japanese  // readable if utf8
something_in_japanese // readable if euc-jp
something_in_japanese // readable if euc-jp
something_in_japanese // readable if shift-jis
something_in_japanese // readable if utf8

This is why i want an easy way to switch between the encodings. Preferably without having to get the data again.  (but that might be hoping for too much.)

The other option is to detect the language of the previous page when the link is input into the database and convert it to UTF8.  Ill explore that option if this one does not have a solution.

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ussher

ASKER

Thanks.

Ok ill look into that option further.  But it is going to have problems too.  Yahoo Japan is euc-jp and i dont think they will change even if i ask nicely. ;)

help appreciated.
How about writing an iframe for each codepage?
Avatar of ussher

ASKER

Problem Sorted

detect the incoming string and convert it before entering it into the database.
<?php
$s = $_GET['tracking_id'];
//detect the encoding
$encoding = mb_detect_encoding($s, "JIS, EUC-JP,UTF-8, SJIS");
$s = mb_convert_encoding($s, 'UTF-8', $encoding);

?>