jQWidon’t - Suppress typographic widows

jQWidon’t is a plugin for the jQuery JavaScript library that replaces the whitespace between the last two words of a given element’s contents with a non-breaking space, to prevent typographic widows. It was inspired by Shaun Inman’s Widon’t plugin for Wordpress. This document describes jQWidon’t version 0.1.

To quote Mr Inman:

For those who don’t know, in typesetting, a widow is a single word on a line by itself at the end of a paragraph and is considered bad style.

Author

Dave Cardwell (jQuery plugins)


Changelist

  1. v0.1 - Saturday 26th August, 2006


Usage

The uncompressed source code is heavily documented. After reading this synopsis, that would be a good place to look.

Initialisation

By default, jQWidon’t will initialise when $(document).ready() is triggered, using the plugin on all heading elements (ie. <h1> through <h6>). You can stop this behaviour by $.jqwidont.auto(false)ing before $(document).ready() is triggered. If you subsequently wish to use the plugin’s default behaviour you can use $.jqwidont.init().

var auto = $.jqwidont.auto(); // is automatic initialisation enabled?
$.jqwidont.auto(false);       // disable it

$.jqwidont.init(); // perform the default behaviour

De-widowing elements

You can attempt to use the plugin on any element that has a $(foo).html() value.

$('p').widont();

De-widowing strings

You can also use the plugin directly on a string.

var string = $.jqwidont.transform('No widows here!'); // string = 'No widows&#160;here!'

Notes

While jQWidon’t could theoretically be used on any element, it was designed for preventing unsightly widows from occuring in a page’s headers. In many cases the non-breaking space would be better hard-coded on the server side, or from the use of a plugin like Widon’t. This plugin is for those times when that’s not feasible, or you want to ensure content generated dynamically by JavaScript is free from widows.

If you change the contents of an element you will have to manually perform another $(foo).widont() call on it.


Download

jQWidon’t is dual-licensed under the MIT and GNU GPL licenses, so if you can use jQuery you can use this plugin.

If you find a use for jQWidon’t I’d love to hear about it. Also, a link to this site wouldn’t go amiss!

I recommend you download the version that I’ve compressed with Dean Edwards’ packer which weighs in at 0.9K. Alternatively I make the heavily documented, uncompressed version available at 4.4K.


Bugs

jQWidon’t v0.1 has no known bugs.


Testing