This page requires a JavaScript-compatible browser.

JavaScript Demos:
Bad Hemingway Story Creation.

jim.cerny@unh.edu
rev. November 26, 2001

JavaScript

This is your opportunity to face the White-Bull-That-Is-Paper-With-No-Words-On-It.

a drink:
an obscenity:
landscape feature:
another writer:
a European city:
a unit of currency:
time of day:

Discussion.

This idea is taken from the book "The Best of Bad Hemingway," George Plimpton (intro.), [1989], Harcourt Brace Jovanovich, paper, ISBN:0-15-611861-0. That is a collection of parodies of Ernest Hemingway, mostly entries in the Imitation Hemingway Competition sponsored for some years by Harry's Bar & American Grill. Hemingway, BTW, did not like to be parodied. He once said, "The step up from writing parodies is writing on the wall above the urinal." To submit your imitation, see the current competition. And some previous efforts are online. See also the Bad Hemingway page.

The JavaScript in this example is modeled closely on Heinle's "MadLibs" in his Designing with JavaScript book. It shows information entered via simple text on HTML Forms can then be rearranged and output in a new window. The use of JavaScript to create a new window from forms-entered information is the important thing, despite the misdirection applied to your attention by the subject matter.

Please note the following:

  • We use a form to enter information, in this case elements of our story. We can enter as much or as little information as we want. Each item is entered in the FORM as a simple INPUT tag of attribute type TEXT. But notice that a NAME attribute is assigned for later reference in the script.

  • We also assign a name to the FORM itself.

  • The heart of the script is the function "likePapa()" which is invoked at the end of the form by assigning an onClick event handler to an INPUT tag of attribute type BUTTON with a value of CREATE STORY.

  • The "likePapa" function creates a new variable called "story", opens a new window of specified characteristics, and write the story into the window.

  • The "story" variable is composed by concatenating actual HTML tags (coded in the script here in upper case, just as an arbitrary convention), plus fixed strings of text that are story boilerplate, plus interspersed inputs from the form. These inputs are referenced as "document.papa.input1.value", meaning the value of "input1" in the form "papa" in the current document.

  • The new window is specified as 300 pixels wide, 250 pixels high, and with scrollbars. The "libsWin.document.close" statement makes sure that the system knows you are done with that window.

  • There is room for improvement. For example, this script does not check to see if fields on the form are left blank, nor does it assign default values instead of checking.

A more practical use of this technique would be to allow a visitor to confirm data they they'd entered on a form. You can take the information and re-write it for them to look at and allow them a chance to go back and make a change before continuing. That would also allow them to print or save a summary of the activity involved. But that requires some additional techniques, particularly use of the "onSubmit" event handler.

(USA TODAY AWARD)

Return to JavaScript Course page.