急急 动态JS 调用文本 乱码

yfgcq 2007-04-13 08:59:22
动态生成JS document.createElement('script');SRC="xx.txt"调用静态文本
以前是使用ASP FSO生成的静态文本.txt
现在该用了NET生成的静态文本(.txt)(UTF8)
但是现在src="xx.txt"的时候,怎么成了乱码?

调用脚本的页面编码GB2312
是不是在调用的时候,要把.txt转换成GB2312?
如果要转的话 ,怎么转啊?
...全文
401 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yfgcq 2007-04-14
  • 打赏
  • 举报
回复
呵呵
终于自己解决了
我在动态加载的时候,重新指定了个编码utf_8
adverse 2007-04-13
  • 打赏
  • 举报
回复
上面那个有点问题,修改了一下
function chinesefromutf8url(strutf8)
{
var bstr = "";
var noffset = 0;
// processing point on strutf8
if (strutf8 == "")
return "";
strutf8 = strutf8.toLowerCase();
noffset = strutf8.indexOf("%e");
if (noffset == -1)
return strutf8;
while (noffset != -1)
{
bstr += strutf8.substr(0, noffset);
strutf8 = strutf8.substr(noffset, strutf8.length - noffset);
if (strutf8 == "" || strutf8.length < 9) // bad string
return bstr;
bstr += utf8codetochinesechar(strutf8.substr(0, 9));
strutf8 = strutf8.substr(9, strutf8.length - 9);
noffset = strutf8.indexOf("%e");
}
return bstr + strutf8;
}
function unicodefromutf8(strutf8)
{
var bstr = "";
var ntotalchars = strutf8.length; // total chars to be processed.
var noffset = 0; // processing point on strutf8
var nremainingbytes = ntotalchars; // how many bytes left to be converted
var noutputposition = 0;
var icode, icode1, icode2; // the value of the unicode.
while (noffset < ntotalchars)
{
icode = strutf8.charcodeat(noffset);
if ((icode & 0x80) == 0) // 1 byte.
{
if ( nremainingbytes < 1 ) // not enough data
break;
bstr += string.fromcharcode(icode & 0x7f);
noffset++;
nremainingbytes -= 1;
}
else if ((icode & 0xe0) == 0xc0) // 2 bytes
{
icode1 = strutf8.charcodeat(noffset + 1);
if ( nremainingbytes < 2 || (icode1 & 0xc0) != 0x80 ) // invalid pattern
{
break;
}
bstr += string.fromcharcode(((icode & 0x3f) << 6) | ( icode1 & 0x3f));
noffset += 2;
nremainingbytes -= 2;
}
else if ((icode & 0xf0) == 0xe0) // 3 bytes
{
icode1 = strutf8.charcodeat(noffset + 1);
icode2 = strutf8.charcodeat(noffset + 2);
if ( nremainingbytes < 3 || // not enough data
(icode1 & 0xc0) != 0x80 || // invalid pattern
(icode2 & 0xc0) != 0x80 )
{
break;
}
bstr += string.fromcharcode(((icode & 0x0f) << 12) | ((icode1 & 0x3f) << 6) | (icode2 & 0x3f));
noffset += 3;
nremainingbytes -= 3;
}
else // 4 or more bytes -- unsupported
break;
}
if (nremainingbytes != 0)
{
// bad utf8 string.
return "";
}
return bstr;
}
function utf8codetochinesechar(strutf8)
{
var icode, icode1, icode2;
icode = parseInt("0x" + strutf8.substr(1, 2));
icode1 = parseInt("0x" + strutf8.substr(4, 2));
icode2 = parseInt("0x" + strutf8.substr(7, 2));
return String.fromCharCode(((icode & 0x0f) << 12) | ((icode1 & 0x3f) << 6) | (icode2 & 0x3f));
}
alert(chinesefromutf8url("%e6%b5%8b%e8%af%95"))
adverse 2007-04-13
  • 打赏
  • 举报
回复
看看这个js方法好使吗
function chinesefromutf8url(strutf8)
{
var bstr = "";
var noffset = 0;
// processing point on strutf8
if (strutf8 == "")
return "";
strutf8 = strutf8.tolowercase();
noffset = strutf8.indexof("%e");
if (noffset == -1)
return strutf8;
while (noffset != -1)
{
bstr += strutf8.substr(0, noffset);
strutf8 = strutf8.substr(noffset, strutf8.length - noffset);
if (strutf8 == "" || strutf8.length < 9) // bad string
return bstr;
bstr += utf8codetochinesechar(strutf8.substr(0, 9));
strutf8 = strutf8.substr(9, strutf8.length - 9);
noffset = strutf8.indexof("%e");
}
return bstr + strutf8;
}
function unicodefromutf8(strutf8)
{
var bstr = "";
var ntotalchars = strutf8.length; // total chars to be processed.
var noffset = 0; // processing point on strutf8
var nremainingbytes = ntotalchars; // how many bytes left to be converted
var noutputposition = 0;
var icode, icode1, icode2; // the value of the unicode.
while (noffset < ntotalchars)
{
icode = strutf8.charcodeat(noffset);
if ((icode & 0x80) == 0) // 1 byte.
{
if ( nremainingbytes < 1 ) // not enough data
break;
bstr += string.fromcharcode(icode & 0x7f);
noffset++;
nremainingbytes -= 1;
}
else if ((icode & 0xe0) == 0xc0) // 2 bytes
{
icode1 = strutf8.charcodeat(noffset + 1);
if ( nremainingbytes < 2 || (icode1 & 0xc0) != 0x80 ) // invalid pattern
{
break;
}
bstr += string.fromcharcode(((icode & 0x3f) << 6) | ( icode1 & 0x3f));
noffset += 2;
nremainingbytes -= 2;
}
else if ((icode & 0xf0) == 0xe0) // 3 bytes
{
icode1 = strutf8.charcodeat(noffset + 1);
icode2 = strutf8.charcodeat(noffset + 2);
if ( nremainingbytes < 3 || // not enough data
(icode1 & 0xc0) != 0x80 || // invalid pattern
(icode2 & 0xc0) != 0x80 )
{
break;
}
bstr += string.fromcharcode(((icode & 0x0f) << 12) | ((icode1 & 0x3f) << 6) | (icode2 & 0x3f));
noffset += 3;
nremainingbytes -= 3;
}
else // 4 or more bytes -- unsupported
break;
}
if (nremainingbytes != 0)
{
// bad utf8 string.
return "";
}
return bstr;
}
function utf8codetochinesechar(strutf8)
{
var icode, icode1, icode2;
icode = parseint("0x" + strutf8.substr(1, 2));
icode1 = parseint("0x" + strutf8.substr(4, 2));
icode2 = parseint("0x" + strutf8.substr(7, 2));
return string.fromcharcode(((icode & 0x0f) << 12) | ((icode1 & 0x3f) << 6) | (icode2 & 0x3f));
}
alert(chinesefromutf8url("%e6%b5%8b%e8%af%95"))
lizhongbin 2007-04-13
  • 打赏
  • 举报
回复
帮楼主顶。。。
adverse 2007-04-13
  • 打赏
  • 举报
回复
觉得最好还是把.txt文件的编码改了.
用记事本打开,另存为,编码选择ANSI就可以了.
adverse 2007-04-13
  • 打赏
  • 举报
回复
function utf2gb(utfstr)

for dig=1 to len(utfstr)

if mid(utfstr,dig,1)="%" then

if len(utfstr) >= dig+8 then

gbstr=gbstr & convchinese(mid(utfstr,dig,9))

dig=dig+8

else

gbstr=gbstr & mid(utfstr,dig,1)

end if

else

gbstr=gbstr & mid(utfstr,dig,1)

end if

next

utf2gb=gbstr

end function

function convchinese(x)

a=split(mid(x,2),"%")

i=0

j=0

for i=0 to ubound(a)

a(i)=c16to2(a(i))

next

for i=0 to ubound(a)-1

digs=instr(a(i),"0")

unicode=""

for j=1 to digs-1

if j=1 then

a(i)=right(a(i),len(a(i))-digs)

unicode=unicode & a(i)

else

i=i+1

a(i)=right(a(i),len(a(i))-2)

unicode=unicode & a(i)

end if

next

if len(c2to16(unicode))=4 then

convchinese=convchinese & chrw(int("&h" & c2to16(unicode)))

else

convchinese=convchinese & chr(int("&h" & c2to16(unicode)))

end if

next

end function

function c2to16(x)

i=1

for i=1 to len(x)  step 4

c2to16=c2to16 & hex(c2to10(mid(x,i,4)))

next

end function

function c2to10(x)

c2to10=0

if x="0" then exit function

i=0

for i= 0 to len(x) -1

if mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)

next

end function

function c16to2(x)

i=0

for i=1 to len(trim(x))

tempstr= c10to2(cint(int("&h" & mid(x,i,1))))

do while len(tempstr)<4

tempstr="0" & tempstr

loop

c16to2=c16to2 & tempstr

next

end function

function c10to2(x)

mysign=sgn(x)

x=abs(x)

digs=1

do

if x<2^digs then

exit do

else

digs=digs+1

end if

loop

tempnum=x

i=0

for i=digs to 1 step-1

if tempnum>=2^(i-1) then

tempnum=tempnum-2^(i-1)

c10to2=c10to2 & "1" 
else
c10to2=c10to2 & "0"
end if
next
if mysign=-1 then c10to2="-" & c10to2

end function

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧