Javascript

자바스크립트 Text Byte 계산후 글자 잘라내고 ... 붙이기

A.H.N 2013. 6. 24. 11:42

// Text Byte수 계산하여 Text 잘라내기 시작

var totalByte = 0;

for(var i =0; i < _title.length; i++) {

var currentByte = _title.charCodeAt(i);

if(currentByte > 128) {

totalByte += 2;

}

else {

totalByte++;

}

}

var titlebyte = totalByte;

if(titlebyte > 20) { // 타이틀이 20바이트가 넘으면 11바이트까지 표시후 '...'을 뒤에 붙임.

var substr_title = _title.substr(0,11) + "...";

} else { // 타이틀이 20바이트가 넘지 않으면 그대로 표시함.

var substr_title = _title;

}

// Text Byte수 계산하여 Text 잘라내기 끝