function getCharset(){
var paras = document.getElementsByTagName("meta");//js获取所有meta
//var paras=$('meta');//jq获取
var regGbk=/\b(gbk|GB2312)\b/i;//适配gbk或者gbxxxx
var regUtf=/\b(utf8|utf-8)\b/i;//匹配utf-8编码的两种写法
var thisCharset='';
for(var i=0;i<paras.length;i++){
var content =paras[i].getAttribute("content");
var httpEquiv = paras[i].getAttribute("http-equiv");
var charset=paras[i].getAttribute("charset");
if(httpEquiv&&content.match(regGbk)){//gbk
var charset=paras[i].getAttribute("content");
thisCharset='gbk';
}else if(charset&&charset.match(regUtf)){//utf-8
var charset=paras[i].getAttribute("charset");
thisCharset='utf-8';
}
}
return {'charset':thisCharset};
}
getCharset();//{charset: "utf-8"}
获取页面编码格式
最新推荐文章于 2024-01-15 17:42:14 发布