You're looking for
substring.
Code:
<script type="text/javascript" src="http://www.mydomain.com/external.php?type=js"></script>
<script type="text/javascript">
<!--
for (var i = 0; i < threads.length; i++)
{
document.write('<li><a href="http://www.mydomain.com/showthread.php?t=' + threads[i]['threadid'] + '" title="' + threads[i]['title'] + '">' + threads[i]['title'].substr(0, 20) + '</a></li>');
}
//-->
</script>
But that won't look very pretty (normally people expect things that are cut off to end with
ellipsis), I would recommend splitting it up like so...
Code:
<script type="text/javascript" src="http://www.mydomain.com/external.php?type=js"></script>
<script type="text/javascript">
<!--
for (var i = 0; i < threads.length; i++)
{
var url = '<li><a href="http://www.mydomain.com/showthread.php?t=' + threads[i]['threadid'] + '" title="' + threads[i]['title'] + '">';
if(threads[i]['title'].length > 20) {
url += threads[i]['title'].substr(0, 20) + '...';
}
else {
url += threads[i]['title'];
}
url += '</a></li>';
document.write(url);
}
//-->
</script>
That way you get the ellipsis at the end of thread titles that are cut off.