function shorten(string, length){ 
    if (string.length &lt;= length) 
    { 
        return string; 
    }else 
    { 
        count = 0; 
        output = ""; 
        words = string.split(" "); 
        for(i=0; i &lt; words.length; i++) 
        { 
            if (count+5+words[i].length &lt;= length) 
            { 
                if (i != 0) output += " "; 
                output += words[i]; 
                count += words[i].length+1; 
            }else 
            { 
                break; 
            } 
        } 
        return output + " …"; 
    } 
}
