Truncate
Truncate is a JavaScript plugin built for the jQuery library.
It conserves space, preserves HTML, and is only 3KB.
[ v.2.3 ] released February 11, 2008
Overview | Release Notes | Examples | Download
Overview
-
The JavaScript is 3KB, and the plugin is released under the GNU GPL.
-
The plugin has one required option, which is the maximum number of characters you would like to allow in an HTML string before truncating.
-
The plugin has two optional settings that can be modified. These options are chars and trail.
-
chars:
An acceptable set of characters (designated by a regular expression) to truncate in front of, once the max has been reached. If an acceptable character is not found at the max, the plugin will traverse the string backwards until one is found. If none is found, the string will not truncate. The default value is a single white space character.
-
trail:
An array that must contain two string entries (empty or otherwise). The first string is the "trail" that will follow truncated text. This can contain a link to expand the text back to the original HTML. The second string is reserved for a link that allows expanded text to be truncated again. The two class names you must use on these links are truncate_show and truncate_hide.
-
The plugin will clean up any single or empty HTML tag sets at the end of a truncated string, but it will not remove close tags associated with open tags at the beginning of a truncated string.
-
Please view the examples and source to better understand how the settings work.
Release Notes
February 11, 2008, v2.3 Release
-
Supported jQuery Version: [ 1.2.3 ]
-
Supported Browsers: [ Windows IE 6+, FF 1.5+, Opera 9+ ] [ Mac Safari 2+, FF 1.5+, Opera 9+ ]
-
I finally managed to add support for preserving HTML. This was the most commonly suggested feature since the last release. The plugin code has changed significantly, and the major difference is that it now maintains two copies of the HTML string -- the original, and the truncated.
-
Next Release:
If you have ideas for this plugin, please send them my way. I do not have anything in store for the next release at this time, except for any bug fixes.
August 08, 2007, v2.2 Release
-
Supported jQuery Version: [ 1.1.3.1 ]
-
Supported Browsers: [ Windows IE 6+, FF 1.5+, Opera 9+ ] [ Mac Safari 2+, FF 1.5+, Opera 9+ ]
-
I received a few suggestions regarding the ability to expand and collapse the text that was truncated. I was a bit hesitant at first, thinking it would add unnecessary weight. However, I managed to come up with a solution that I think works well, and does not appear to affect performance.
-
Next Release:
Thank you for everyone's comments and suggestions. If you have ideas for this plugin, please send them my way. I do not have anything in store for the next release at this time.
July 24, 2007, v2.1 Release
-
Supported jQuery Version: [ 1.1.3.1 ]
-
Supported Browsers: [ Windows IE 6+, FF 1.5+, Opera 9+ ] [ Mac Safari 2+, FF 1.5+, Opera 9+ ]
-
The JavaScript for the plugin was optimized, and the leave option was added.
-
Next Release:
Unless any improvements are requested, or bugs are discovered, then this will likely be the final release. I tried to provide a mechanism for truncating while preserving embedded HTML tags, but it proved to be unmanageable in every scenario. From the feedback I have received so far, the current iteration appears to be sufficient.
Examples
1. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec a lorem. Maecenas vel risus et eros vulputate sagittis. Aliquam at tortor. Nunc sit amet elit sit amet quam pellentesque condimentum. Etiam id nulla vitae massa imperdiet varius. Duis a quam. Duis faucibus lobortis velit.
2. Nullam ac ligula. Nunc bibendum massa id purus. Nunc et ipsum. Donec tincidunt erat ac enim. Vivamus vulputate sapien ut nibh. Phasellus nonummy massa vitae lacus. Cras mauris. Praesent vitae purus. Nullam id risus ut purus scelerisque sollicitudin. Aenean tempor ultricies nisl. Aliquam varius leo eu quam. Etiam vitae massa. Donec dui nisi, rutrum sed, commodo sed, ullamcorper eget, purus. Fusce malesuada vestibulum felis.
3. Etiam id nulla vitae massa imperdiet varius. Duis a quam.
- Duis faucibus lobortis velit.
- In lacus sem, elementum nec, tempus id, tempor in, risus.
Morbi rutrum lorem id mauris. Nunc et ipsum.
Donec tincidunt erat ac enim. Vivamus vulputate sapien ut nibh. Phasellus nonummy massa vitae lacus. Cras mauris. Praesent vitae purus.
4. Etiam id nulla vitae massa imperdiet varius. Duis a quam. Duis faucibus lobortis velit. In lacus sem, elementum nec, tempus id, tempor in, risus. Morbi rutrum lorem id mauris. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec a lorem.
Download
The packed version of the jQuery library is bundled with both the packed and unpacked version of the downloads.
Download Truncate (Unpacked)
Download Truncate (Packed)
Please note: I would like to devote more attention to this plugin, but I don't have time to offer support. If you have questions, I strongly encourage you to visit the jQuery Google Group.