View Full Version : How to truncate and add ellipsis

03-03-2011, 06:45 PM

You'll have to forgive me. I'm still pretty much a beginner when it come to js and jQuery, so bare with me please.

I'm trying to embed a tumblr blog into a website using the following code:

<script type="text/javascript">
function tumblr(resp) {
var posts = resp.posts;
$('.blog .loading').replaceWith('<ul class="blogcontent"/>');
$ul = $('.blog ul');
for (var i=0; i<posts.length; i++) {
var p = posts[i];
var date = p['date'];
var title = p['regular-title'] || p['link-text'] || null;
var body = p['regular-body'];
if (date)
if (title)
$ul.append('<a href="'+p['url']+'" target="_blank"> <h2>'+title+'</h2></a>');
if (body)
$ul.append('<a href="'+p['url']+'" target="_blank"> <p>'+body+'</p> </a></li>');

The posts are displayed no problem. But this is where I get lost. If the 'body' copy is longer than, let's say, 30 words (which all of the posts are), I'd like it to be truncated and replaced with '... Read more >" You get the idea.

I've found a few examples online (unrelated to tumblr embedding) but the code is so detailed and interwoven, and me being the newbie that I am, I don't know which bits I need and which I don't.

Could someone point me in the right direction, given the code that I already have?

Any help is greatly appreciated. Thanks.