[Javascript] 글자 지정 길이로 자르기

function cutStr(str,limit) {
var tmpStr = str;
var byte_count = 0;
var len = str.length;
var dot = “”;
for(i=0; i<len; i++){
byte_count += chr_byte(str.charAt(i));
if(byte_count == limit-1){
if(chr_byte(str.charAt(i+1)) == 2){
tmpStr = str.substring(0,i+1);
dot = “…”;
} else {
if(i+2 != len) dot = “…”;
tmpStr = str.substring(0,i+2);
}
break;
} else if(byte_count == limit){
if(i+1 != len) dot = “…”;
tmpStr = str.substring(0,i+1);
break;
}
}
document.writeln(tmpStr+dot);
return true;
}
function chr_byte(chr){
if(escape(chr).length > 4) return 2;
else return 1;
}