Posted By

Jamie on 07/26/11


Tagged

trim String truncate


Versions (?)

Adaptation of Prototype's Truncate function


 / Published in: JavaScript
 

URL: https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js

I just ripped it from Prototype for use in my jQuery based code.

  1. function truncate(text, length, truncation) {
  2. length = length || 30;
  3. truncation = (typeof(truncation) == 'undefined') ? '...' : truncation;
  4. return text.length > length ?
  5. text.slice(0, length - truncation.length) + truncation : String(text);
  6. }
  7.  
  8. truncate("Hello World",10,'...');

Report this snippet  

You need to login to post a comment.