utf-8页面展示 gbk编码的js,gbk中有中文
2020-11-15 18:09
标签:com http blog style class div img code java log javascript index.html 1.js (编码方式
utf-8) 2.js utf-8页面展示 gbk编码的js,gbk中有中文,搜素材,soscw.com utf-8页面展示 gbk编码的js,gbk中有中文 标签:com http blog style class div img code java log javascript 原文地址:http://www.cnblogs.com/g2star/p/3698917.htmlDOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w3.org/1999/xhtml">
head>
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
title>星月随笔title>
head>
body>
这个页面中的 charset编码可以变成其他的。utf-8
script src="1.js">script>
body>
html>
function getPageCharset(){
var charSet = "";
var oType = getBrowser();
switch(oType){
case "IE":
charSet = document.charset;
break;
case "FIREFOX":
charSet = document.characterSet;
break;
default:
charSet = document.charset;
break;
}
return charSet;
}
function getBrowser(){
var oType = "";
if(navigator.userAgent.indexOf("MSIE")!=-1){
oType="IE";
}else if(navigator.userAgent.indexOf("Firefox")!=-1){
oType="FIREFOX";
}
return oType;
}
function GB2312UTF8(){
this.Dig2Dec=function(s){
var retV = 0;
if(s.length == 4){
for(var i = 0; i ){
retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
}
return retV;
}
return -1;
}
this.Hex2Utf8=function(s){
var retS = "";
var tempS = "";
var ss = "";
if(s.length == 16){
tempS = "1110" + s.substring(0, 4);
tempS += "10" + s.substring(4, 10);
tempS += "10" + s.substring(10,16);
var sss = "0123456789ABCDEF";
for(var i = 0; i ){
retS += "%";
ss = tempS.substring(i * 8, (eval(i)+1)*8);
retS += sss.charAt(this.Dig2Dec(ss.substring(0,4)));
retS += sss.charAt(this.Dig2Dec(ss.substring(4,8)));
}
return retS;
}
return "";
}
this.Dec2Dig=function(n1){
var s = "";
var n2 = 0;
for(var i = 0; i ){
n2 = Math.pow(2,3 - i);
if(n1 >= n2){
s += ‘1‘;
n1 = n1 - n2;
}
else
s += ‘0‘;
}
return s;
}
this.Str2Hex=function(s){
var c = "";
var n;
var ss = "0123456789ABCDEF";
var digS = "";
for(var i = 0; i ){
c = s.charAt(i);
n = ss.indexOf(c);
digS += this.Dec2Dig(eval(n));
}
return digS;
}
this.Gb2312ToUtf8=function(s1){
var s = escape(s1);
var sa = s.split("%");
var retV ="";
if(sa[0] != ""){
retV = sa[0];
}
for(var i = 1; i ){
if(sa[i].substring(0,1) == "u"){
retV += this.Hex2Utf8(this.Str2Hex(sa[i].substring(1,5)));
if(sa[i].length){
retV += sa[i].substring(5);
}
}else{
retV += unescape("%" + sa[i]);
if(sa[i].length){
retV += sa[i].substring(5);
}
}
}
return retV;
}
this.Utf8ToGb2312=function(str1){
var substr = "";
var a = "";
var b = "";
var c = "";
var i = -1;
i = str1.indexOf("%");
if(i==-1){
return str1;
}
while(i!= -1){
if(i){
substr = substr + str1.substr(0,i-1);
str1 = str1.substr(i+1,str1.length-i);
a = str1.substr(0,2);
str1 = str1.substr(2,str1.length - 2);
if(parseInt("0x" + a) & 0x80 == 0){
substr = substr + String.fromCharCode(parseInt("0x" + a));
}
else if(parseInt("0x" + a) & 0xE0 == 0xC0){ //two byte
b = str1.substr(1,2);
str1 = str1.substr(3,str1.length - 3);
var widechar = (parseInt("0x" + a) & 0x1F) ;
widechar = widechar | (parseInt("0x" + b) & 0x3F);
substr = substr + String.fromCharCode(widechar);
}
else{
b = str1.substr(1,2);
str1 = str1.substr(3,str1.length - 3);
c = str1.substr(1,2);
str1 = str1.substr(3,str1.length - 3);
var widechar = (parseInt("0x" + a) & 0x0F) ;
widechar = widechar | ((parseInt("0x" + b) & 0x3F) );
widechar = widechar | (parseInt("0x" + c) & 0x3F);
substr = substr + String.fromCharCode(widechar);
}
}
else {
substr = substr + str1.substring(0,i);
str1= str1.substring(i);
}
i = str1.indexOf("%");
}
return substr+str1;
}
}
function gb2utf8(s)
{
var xx=new GB2312UTF8();
return xx.Gb2312ToUtf8(s);
}
var kk=getPageCharset();
//alert(kk) 包含js的页面编码格式 GBK 或者 UTF-8
//alert(decodeURI(encodeURI("星月随笔")))
if(kk=="GBK"||kk=="gb2312"){ //浏览器兼容模式和快速浏览模式得到的编码方式有区别。
document.write(""+"script>") // 2.js 的编码方式必须是GBK,不然显示乱码
}else{
document.write(decodeURI(gb2utf8("
document.write("
上一篇:Jquery属性练习
文章标题:utf-8页面展示 gbk编码的js,gbk中有中文
文章链接:http://soscw.com/index.php/essay/21491.html