본문 바로가기

프로그래밍/JavaScript

[JavaScript] 글자수 자르기





JavaScript를 활용한 글자수 자르기



메인화면에 나타낼 수 있는 글자수는 한정적이다.



특정 글자수가 넘어가면 다른문자로 대체

1
2
3
4
5
6
7
8
9
10
11
12
function textLengthOverCut(txt, len, lastTxt) {
    if (len == "" || len == null) { // 기본값
        len = 20;
    }
    if (lastTxt == "" || lastTxt == null) { // 기본값
        lastTxt = "...";
    }
    if (txt.length > len) {
        txt = txt.substr(0, len) + lastTxt;
    }
    return txt;
}