ussher
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.
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.
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.getElementsByTagN ame('meta' )[0].conte nt.split(' charset=') [1];
var charSets = new Array(
"windows-1250",
"iso-8859-1",
"UTF-8"
);
function changeMeta(idx) {
var met = document.getElementsByTagN ame('meta' );
var newSet = met[0].content.replace(sav eCharset,c harSets[id x]);
saveCharset=charSets[idx];
met[0].content=newSet;
alert('Changed to '+document.getElementsByTa gName('met a')[0].con tent.split ('charset= ')[1])
// document.body.innerHTML=do cument.bod y.innerHTM L;
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><b r>
éàéöä
</body>
</html>
<!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.getElementsByTagN
var charSets = new Array(
"windows-1250",
"iso-8859-1",
"UTF-8"
);
function changeMeta(idx) {
var met = document.getElementsByTagN
var newSet = met[0].content.replace(sav
saveCharset=charSets[idx];
met[0].content=newSet;
alert('Changed to '+document.getElementsByTa
// document.body.innerHTML=do
return false;
}
</script>
</head>
<body >
<a href="#" onClick="return changeMeta(0)">1250</a><br
<a href="#" onClick="return changeMeta(1)">8859-1</a><
<a href="#" onClick="return changeMeta(2)">UTF-8</a><b
éàéöä
</body>
</html>
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_track ing_script .php?track ing_id=som ething_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.
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_track
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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?
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);
?>
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);
?>
Otherwise if possible, you need to change the meta tag - not even sure if that will do it.