var contentBox = $('#target');
//get the text as an array of word-like things
var words = contentBox.text().split(' ');
function paginate() {
//create a div to build the pages in
var newPage = $('<div class="individualPage" />');
contentBox.empty().append(newPage);
//start off with no page text
var pageText = null;
for(var i = 0; i < words.length; i++) {
//add the next word to the pageText
var betterPageText = pageText ? pageText + ' ' + words[i]
: words[i];
newPage.text(betterPageText);
//Check if the page is too long
if(newPage.height() > $(window).height()) {
//revert the text
newPage.text(pageText);
//and insert a copy of the page at the start of the document
newPage.clone().insertBefore(newPage);
//start a new page
pageText = null;
} else {
//this longer text still fits
pageText = betterPageText;
}
}
}
$(window).resize(paginate).resize();