tags:

views:

76

answers:

2
A: 

Here's a handy truncating function I use.

// Examples
truncate('abcdefghijklmnopqrstuvwxyz'); // returns 'abcdefghijklmnopqrst...'
truncate('hello there', 15); // returns 'hello there'
truncate('hello there', 5, '...read more...'); // returns 'hello...read more...'

// Truncating method
function truncate(string, length, end)
{
  if (typeof length == 'undefined')
  {
 length = 20;
  }

  if (typeof end == 'undefined')
  {
 end = '...';
  }

  if (string == null)
  {
 return '';
  }

  return string.substring(0, length-1)+(string.length > length ? end : '');
}
James Skidmore
good helpful function, but im afriad its not for my use yet, because i need to trancate links, which are inside the textarea and not truncate complete text
Basit
+1  A: 

To truncate a string, have a look at the trunc-prototype method for strings in my answer here. To acquire all links of a page use:

var linksHere = document.getElementsByTagName('a');

loop through your links and shorten the innerHTML of every link if the length is more than you want. Something like:

var i=-1,len = linksHere.length;
while (++i<len){
     linksHere[i].innerHTML = linksHere[i].innerHTML.trunc(30);
}
KooiInc
+1. Although I'd keep the loop-housekeeping side-effects outside the condition for clarity.
PatrikAkerstrand