Skip to content

Instantly share code, notes, and snippets.

@andyvanee
Last active September 25, 2015 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyvanee/b32fc85986753deed03e to your computer and use it in GitHub Desktop.
Save andyvanee/b32fc85986753deed03e to your computer and use it in GitHub Desktop.
jQuery plugin to remove widows

Usage

Apply the plugin to whatever tags you want:

$('h1, p').widows();

Be sure that the tags will only contain text content. This will strip HTML content out of the tags passed into it.

(function($){
$.fn.widows = function(){
$(this).each(function(){
$(this).html(
$(this).html().replace(
/ ([^ <]*)([\<\/\w*\>]*)$/,
function(_, last, tag) { return '\xa0' + last + tag }
)
);
})
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment