Sunday, August 08, 2010 3:33 AM
While working on my upcoming cool project, I wanted a JavaScript function that will perform an Ellipsis, bare in mind that I usually use C# and already have a .NET method that will do just that. However, in this case since my web application is 90% JS, I figured I could just do that client side. Here is what I came up with, I just wrote this 5 minutes ago, so use at your own risk.
Ellipsis (plural ellipses; or "three little points of suspension"; from the Greek: ἔλλειψις, élleipsis, "omission") is a mark or series of marks that usually indicate an intentional omission of a word in the original text.
String.prototype.ellipsisText = function(location, maxCharacters) {
var text = this;
if (this.length > 0 && this.length > maxCharacters) {
if (typeof (location) == 'undefined') location = 'end';
switch (location) {
case 'center':
var center = (maxCharacters / 2);
text = this.slice(0, center) + '...' + this.slice(-center);
break;
case 'end':
text = this.slice(0, maxCharacters - 3) + '...';
break;
}
}
return text;
}