<html>
<head>
<script>
function lenByte(tempvalue){
if(tempvalue==null || tempvalue.length==0 || tempvalue=="")
{
return 0;
}else{
var i,sum;
sum=0;
for(i=0;i<tempvalue.length;i++)
{
if ((tempvalue.charCodeAt(i)>=0) && (tempvalue.charCodeAt(i)<=255))
sum=sum+1;
else
sum=sum+2;
}
return sum;
}
}
</script>
</head>
<body>
<script>
var str = "中华人民共和国中华人民共和国中华人民共和";
alert(str.length);
alert(lenByte(str));
</script>
</body>
<head>
<script>
function lenByte(tempvalue){
if(tempvalue==null || tempvalue.length==0 || tempvalue=="")
{
return 0;
}else{
var i,sum;
sum=0;
for(i=0;i<tempvalue.length;i++)
{
if ((tempvalue.charCodeAt(i)>=0) && (tempvalue.charCodeAt(i)<=255))
sum=sum+1;
else
sum=sum+2;
}
return sum;
}
}
</script>
</head>
<body>
<script>
var str = "中华人民共和国中华人民共和国中华人民共和";
alert(str.length);
alert(lenByte(str));
</script>
</body>
</html>
来源:http://blog.youkuaiyun.com/zlcoy_1/article/details/7245818