Shorten string in javascript


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. shortenString : function( str, length ){
  2.  
  3. var newStr = str;
  4. var words = str.split('', length + 1);
  5. if( words.length > length){
  6. words.pop();
  7. words.push('...');
  8. newStr = words.join('');
  9. }
  10. return newStr;
  11. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.