JavaScript - Create excerpt


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



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.