a jQuery "widon't" snippet

in the interest of more attractive internets, here's a quick little javascript "widon't" snippet—written in jQuery—that i've been using. now go do something cool with it.

jQuery('h1,h2,h3,li,p').each(
    function(){
        this.innerHTML = this.innerHTML.replace(/\s([^\s>]+)\s*$/,' $1');
    }
);

feel free to replace or augment the selector ('h1,h2...p') with any additional elements you'd like to see widon'ted.

i actually use this variant, which lets longer words be widows if they wanna:

jQuery('h1,h2,h3,li,p').each(
    function(){
        this.innerHTML = this.innerHTML.replace(/\s([^\s>]{0,10})\s*$/,' $1');
    }
);

note: dave cardwell posted a jQuery widon't a couple of years ago, but holy balls it's big... if you want something a bit more robust, be sure to check his out.

another note: i added newlines and tabs to make the code more readable. if i were you, i'd remove those bad boys before deploying.

Comments

Nice one. Similar to my post last month: Three Quick Ways to Avoid Widows.