同问还有就是用form的get方式提交后地址栏的URL的中文被编码了,但不是utf-8的,如“餐”字,在地址栏中变成%B2%CD%。研究了一下,用以下方式转换,都不是:
escape("餐")=%u9910,encodeURIComponent("餐")=%E9%A4%90,"餐".charCodeAt(0)=39184,toUTF8("餐")=%e9%a4%90,encode_utf8("餐")=%e9%a4%90,URLEncoding("餐")=%99%10
后面几个是自定义的编码函数。function toUTF8(a){
var wch,x,uch="",szRet="";
for (x=0; x<a.length; x++){
wch=a.charCodeAt(x);
if (!(wch & 0xFF80)){
szRet += a.charAt(x);
}
else if (!(wch & 0xF000)){
uch = "%" + (wch>>6 | 0xC0).toString(16) +
"%" + (wch & 0x3F | 0x80).toString(16);
szRet += uch;
}
else{
uch = "%" + (wch >> 12 | 0xE0).toString(16) +
"%" + (((wch >> 6) & 0x3F) | 0x80).toString(16) +
"%" + (wch & 0x3F | 0x80).toString(16);
szRet += uch;
}
}
return(szRet);
}
function encode_utf8(a) {
a = a.replace(/\r\n/g,"\n");
var utftext = "";
for(var n=0; n<a.length; n++)
{
//
var c=a.charCodeAt(n);
// 0-127 => 1byte
if (c<128)
utftext += String.fromCharCode(c);
// 127 - 2047 => 2byte
else if((c>127) && (c<2048)) {
utftext += "%"+((c>>6)|192);
utftext += "%"+((c&63)|128);}
// 2048 - 66536 => 3byte
else {
utftext += "%"+((c>>12)|224).toString(16);
utftext += "%"+(((c>>6)&63)|128).toString(16);
utftext += "%"+((c&63)|128).toString(16);}
}
return utftext;
}
function URLEncoding(a)
{
a = a.replace(/\r\n/g,"\n");
var utftext = "";
for(var n=0; n<a.length; n++)
{
var c=a.charCodeAt(n);
if (Math.abs(c)<256)
{
utftext += String.fromCharCode(c);
}
else
{
if(c<0)
{
c=c+0x10000;
}
utftext += "%"+(c>>8).toString(16);
utftext += "%"+(c&0xFF).toString(16);
}
}
return utftext;
}
escape("餐")=%u9910,encodeURIComponent("餐")=%E9%A4%90,"餐".charCodeAt(0)=39184,toUTF8("餐")=%e9%a4%90,encode_utf8("餐")=%e9%a4%90,URLEncoding("餐")=%99%10
后面几个是自定义的编码函数。function toUTF8(a){
var wch,x,uch="",szRet="";
for (x=0; x<a.length; x++){
wch=a.charCodeAt(x);
if (!(wch & 0xFF80)){
szRet += a.charAt(x);
}
else if (!(wch & 0xF000)){
uch = "%" + (wch>>6 | 0xC0).toString(16) +
"%" + (wch & 0x3F | 0x80).toString(16);
szRet += uch;
}
else{
uch = "%" + (wch >> 12 | 0xE0).toString(16) +
"%" + (((wch >> 6) & 0x3F) | 0x80).toString(16) +
"%" + (wch & 0x3F | 0x80).toString(16);
szRet += uch;
}
}
return(szRet);
}
function encode_utf8(a) {
a = a.replace(/\r\n/g,"\n");
var utftext = "";
for(var n=0; n<a.length; n++)
{
//
var c=a.charCodeAt(n);
// 0-127 => 1byte
if (c<128)
utftext += String.fromCharCode(c);
// 127 - 2047 => 2byte
else if((c>127) && (c<2048)) {
utftext += "%"+((c>>6)|192);
utftext += "%"+((c&63)|128);}
// 2048 - 66536 => 3byte
else {
utftext += "%"+((c>>12)|224).toString(16);
utftext += "%"+(((c>>6)&63)|128).toString(16);
utftext += "%"+((c&63)|128).toString(16);}
}
return utftext;
}
function URLEncoding(a)
{
a = a.replace(/\r\n/g,"\n");
var utftext = "";
for(var n=0; n<a.length; n++)
{
var c=a.charCodeAt(n);
if (Math.abs(c)<256)
{
utftext += String.fromCharCode(c);
}
else
{
if(c<0)
{
c=c+0x10000;
}
utftext += "%"+(c>>8).toString(16);
utftext += "%"+(c&0xFF).toString(16);
}
}
return utftext;
}
解决方案 »
- 转码 问题求教
- javascript:setTimeout()和setinterval()函数是什么?
- 为什么jquery代码在IE下能执行,可是在IE内核的360,搜狗浏览器中却没反应
- js的call()的this指向的一些诱惑!
- select option中有中文, 滚动条就失效了??急急急急!!!!
- 无聊,修改了下前几天发布的那个小东西
- 动态计算文本框内输入字符的数目为什么少一个?
- 打开一个窗口的问题,高手才能进!
- 在线求助!小弟不胜感激!!!!
- 用什么属性可以设置<select>或<input type=file>不可用和可用?
- 帮帮忙呀,想到头都大了.
- 如何在弹出的新窗口中的多个frame中同时引用传过来的值。
<script language='javascript'>
function cc(e){
alert(escape("中文"));//将中文字符转为unicode码
aa.value = escape("中文");
alert(unescape("escape("中文")"));//解码
}
</script>可以试一下,不知道是否能够解决楼主问题
<SCRIPT language=vbscript>
function vbGetGBCode(str)
dim i, length, s
length = len(str) for i=1 to length
hexCode = hex(asc(str.charAt(i-1)))
if (len(hexCode) mod 2) = 0 then
s = s & hexCode
else
s = s & "0" & hexCode
end if
next
vbGetGBCode = s
end function
</SCRIPT><SCRIPT language=javascript>
String.prototype.getGBCode = function()
{
return vbGetGBCode(this);
}
</SCRIPT>
String.prototype.getString = function()
{
return vbGetString(this);
}
</SCRIPT><SCRIPT language=vbscript>
function vbGetString(hexString)
dim i, length, s
length = Len(hexString)/2
for i=1 to length
charCode = jsParseInt(Mid(hexString, i*2-1, 2), 16)
if charCode < &H7F then
s = s & Chr(charCode)
else
s = s & Chr(jsParseInt(Mid(hexString, i*2-1, 4), 16))
i = i+1
end if
next
vbGetString = s
end function
</SCRIPT>