?
Solved

javascript change character encoding

Posted on 2007-10-01
7
Medium Priority
?
9,408 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:ussher
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

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

Expert Comment

by:Michel Plungjan
ID: 19997255
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>
0
 
LVL 1

Author Comment

by:ussher
ID: 19997313
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.

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1260 total points
ID: 19997380
I absolutely support the last suggestion along with suggesting the participating sites to go UTF-8
0
 
LVL 1

Author Comment

by:ussher
ID: 19997413
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.
0
 
LVL 75

Expert Comment

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

Author Comment

by:ussher
ID: 20004309
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);

?>
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 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