PDA

View Full Version : How do I limit the characters in JS


r0r0
12-10-2009, 11:11 PM
So I'm using this as a latest post module,

<script type="text/javascript" src="forums/external.php?type=js"></script>

<script language="" type="text/javascript">

<!--
for (x = 0; x < 5; x++)
{
document.writeln("<li><a href=\"showthread.php?t="+threads[x].threadid+"\">"+threads[x].title+"</a><span>By: "+threads[x].poster+")</span><br /></li>");
}
//-->
</script>

What I would like to is limit the thread title to certain amount characters,

so for example:

Welcome to our forums, please read our forum rules before you post.

should output,

Welcome to our forums, please read our forum...

Thanks

kh99
12-10-2009, 11:26 PM
Maybe:


var maxTitle = 20;
for (x = 0; x < 5; x++)
{
var title = threads[x].title;
if (title.length > maxTitle )
{
title = title.substr(0,maxTitle-3) + "...";
}
document.writeln("<li><a href=\"showthread.php?t="+threads[x].threadid+"\">"+title+"</a><span>By: "+threads[x].poster+")</span><br /></li>");
}

But I'm not a JS expert and I didn't try that, so there could be some error in it.

r0r0
12-10-2009, 11:28 PM
Thanks worked perfectly!