Go to home page mail me! RSS Feed
FoxMetrics Web Analytics

A quick and dirty JavaScript Ellipsis function

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;
}
DotNetKicks Image

Your Comments.

No comments posted yet.

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 1 and 7 and type the answer here:

Preview Your Comment.

 
Next entries »