javascript change character encoding

Posted on 2007-10-01
Last Modified: 2008-01-09

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

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.
Question by:ussher
    LVL 75

    Expert Comment

    by:Michel Plungjan
    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.
    LVL 75

    Expert Comment

    by:Michel Plungjan
    Does not seem writeable

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <meta http-equiv="content-type" content="text/html; charset=windows-1250">
      var saveCharset = document.getElementsByTagName('meta')[0].content.split('charset=')[1];
      var charSets = new Array(
      function changeMeta(idx) {
        var met = document.getElementsByTagName('meta');
        var newSet = met[0].content.replace(saveCharset,charSets[idx]);
        alert('Changed to '+document.getElementsByTagName('meta')[0].content.split('charset=')[1])
    //    document.body.innerHTML=document.body.innerHTML;
        return false;
      <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>

    LVL 1

    Author Comment

    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.


    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.

    LVL 75

    Accepted Solution

    I absolutely support the last suggestion along with suggesting the participating sites to go UTF-8
    LVL 1

    Author Comment


    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.
    LVL 75

    Expert Comment

    by:Michel Plungjan
    How about writing an iframe for each codepage?
    LVL 1

    Author Comment

    Problem Sorted

    detect the incoming string and convert it before entering it into the database.
    $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);


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
    JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
    The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
    The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now