The first time I tried to use Chris’ snippet, I tried to apply it to all my Joomla titles in a template I was building, not realizing that there are a few conditions that render the script ineffective.
My original selector looked something like this:
1 |
And here is what I noticed afterwards:
So to fix both issues, we just have to account for those possibilities:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $("h1, h2, h3, h4, h5, h6, .contentheading, .componentheading").each(function() { if($(this).has('a')) { var wordArray = $('a', this).text().split(" "); if(wordArray.length > 1) { wordArray[wordArray.length-2] += " " + wordArray[wordArray.length-1]; wordArray.pop(); $('a', this).html(wordArray.join(" ")); } } else { var wordArray = $(this).text().split(" "); if(wordArray.length > 1) { wordArray[wordArray.length-2] += " " + wordArray[wordArray.length-1]; wordArray.pop(); $(this).html(wordArray.join(" ")); } } }); |
Note that the same can be true for small, strong, em, and other tags, etc. The point is just to be careful.
Here’s Chris’ original article on preventing title widows just for reference.
Say you have a box (div) displaying a different testimonial every time the page is loaded. And you want two other boxes to be the same height as the one with the variable size testimonial.

* Note that if your content contains images, you will want to run the script once the window is loaded instead.
Nice & easy:
1 2 3 4 5 6 7 8 | var maxHeight = 0; $('.box').each(function() { var boxHeight = $(this).height(); if(boxHeight > maxHeight) maxHeight = boxHeight; }); $('.box').css({ height : (maxHeight + 'px') }); |
I will actually work on a example integrating WordPress into the equation and update this article as soon as I get some spare time in my hands.
If you have a more efficient method, please comment away! Otherwise, I hope you find this useful.
Listen to what others have to say about their experience working with me.
Which of the following do you think will have a greater impact on your final decision of hiring a particular web designer?
Learn a little about how I do what I love, what makes me unique and my work ethics.