javascript limit display link from text area
i have a following feed from twitter and im making all links clickable and then i want that links which are inside a tag to be short to 30 chars, if its more then 30 chars then show ... after 30 chars
twitter feed
i need to start learning some real javascript from http://javascript.com/java/codes/snippet/search?q=javascript+limit+chars+leading some more text here, so dont remove this.
TO
i need to start learning some 开发者_C百科real javascript from http://javascript.com/java... some more text here, so dont remove this.
just need to know how can i truncate the inside of tag.
Edited
the link can be anywhere in whole text area.
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);
}
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 : '');
}
精彩评论