Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tjhole/7890717 to your computer and use it in GitHub Desktop.
Save tjhole/7890717 to your computer and use it in GitHub Desktop.
JQUERY: Add non-breaking space to titles to prevent widows
$structure(document).ready(widow);
function widow(){
$structure("h1,h2,h3,h4,h5").each(function() {
var wordArray = $structure(this).text().split(" ");
var finalTitle = "";
for (i=0;i<=wordArray.length-1;i++) {
finalTitle += wordArray[i];
if (i == (wordArray.length-2)) {
finalTitle += "&nbsp;";
} else {
finalTitle += " ";
}
}
$structure(this).html(finalTitle);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment