"My First Law of Distributed Object Design:
Don't distribute your objects."
--Martin Fowler

Lorem Ipsum Filler Text in Javascript.

I do page layouts for websites sometimes and I often find myself going to, in order to find satisfactory filler text. Today I needed something a little more dynamic for testing a fluid page layout. I ended up writing a javascript function that you can call from within your page in order to dynamically create some filler text. Saves me a trip to, and is easy to use. it's been added to my standard debug.js file. Here it is…

<script type="text/javascript">
var chunkCount = 5;
var minChunksPerPara = 3;
var chunk1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
var chunk2 = 'Nunc vitae dignissim turpis. Nulla eleifend felis sed velit molestie non fermentum nibh pellentesque.';
var chunk3 = 'Duis egestas dapibus felis ut posuere.';
var chunk4 = 'Ut nec neque eu lacus pulvinar commodo et vel diam. Integer semper adipiscing enim eu tempus.';
var lorem = ['p', chunk1, chunk2, chunk3, chunk4];
/* The number of chunks written to the current paragraph. */
var paraChunkCount = 0;
/* Minimum number of chunks to write per paragraph. Larger number means larger paragraphs. */
function fillLorem(chunks) {
	document.write('<p>' + chunk1 + ' ');
	for (var i = 0; i < chunks; i++) {
		var chunk = lorem[Math.floor(Math.random() * chunkCount)];
		if (chunk == 'p') {
			i--; /* decrement i, or else we'll lose a printed chunk for every p. */
			if (paraChunkCount >= minChunksPerPara) {
				/* we're writing the end of a paragraph and starting a new one. */
				paraChunkCount = 0;
				paraChunkCount = 0;
		else {
			/* We're writing the current chunk to the current paragraph. */
			document.write(chunk + ' ');

…and here is how to use it:

<script type="text/javascript">fillLorem(50)</script>

That will create a filler text block made up of a random selection of fifty of chunk1 through chunk4. If you want a more natural looking Lipsum (fewer repeated passages), increase the number of chunks to something greater than four, and change the chunkCount variable to one greater than the number of chunks you have (one greater, because the 'p' is a chunk and needs to be represented in chunkCount).