Posted By

jonniespratley on 07/24/09


Tagged

text


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

thastyle


JavaScript - Create excerpt


 / Published in: JavaScript
 

  1. /**
  2.  * I take a string and create an excerpt from it, returning an object
  3.  * with the summary and body properties,
  4.  * @param {Object} str - the string to shorten
  5.  * @param {Object} limit - the limit of characters
  6.  */
  7. function jps_shortString( str, limit )
  8. {
  9. var body = new String( str );
  10. var summary = new String(str);
  11. summary = summary.substr( 0, summary.lastIndexOf( ' ', limit ) ) + '...';
  12.  
  13. var returnString = new Object({
  14. body: body,
  15. summary: summary
  16. });
  17.  
  18. //window.console.log( 'Summary - ' + summary);
  19. //window.console.log( 'Body - ' + body);
  20.  
  21. return returnString;
  22. }

Report this snippet  

You need to login to post a comment.