Dialogs from JavaScript in a PDF Form – User interaction

In a previous column, we talked about how to pop an alert from within a PDF form using

JavaScript (that is, a JavaScript action attached to a form field). Now, we’ll expand on

that by talking about how to pop a response dialog. The difference between an alert and a

response dialog is that the former basically only allows for one type of response: hitting

OK. (Actually, you can also have a Cancel button, or a Yes and a No button, or Yes, No, and

Cancel.) But the point is, the user’s possible replies are limited in nature. There’s not

much of a chance for the user to say anything.

Sometimes you want to be able to query the user for additional info, beyond what’s

already being asked for in the form itself. For example, suppose you have a form field that

asks for the user’s city, state, and Zip code. Perhaps you have a script that checks the

user’s city against the Zip code in a large database, to see that the city correctly

corresponds to the Zip code that the user inputs. In the event of a discrepancy, you could

pop a dialog that asks the user to confirm the spelling of his city. The user could then

type the correct city name into the dialog, and hit OK. This type of information-retrieval

dialog is called a response dialog, and you can put one up at runtime by calling on the

response() method of the App Object, like so:

An example

answer = app.response('Are you sure you want overnight shipping?','ARE YOU SURE?','To


The response() method displays a dialog box containing a question and an entry field for

the user to reply to the question. Optionally, the dialog may have a title or a default

value for the answer to the question. The return value is a string containing the user’s

response. If the user presses the cancel button on the dialog, the return value is nil.

Notice that we supplied a title argument (which shows up as the name of the response

dialog’s window) and a default reply, which shows up as highlighted text in the answer box.

(On a Mac, the response dialog is actually a Movable Modal dialog. You can drag it around

the screen.) If the user cancels out of the dialog, the return value will be nil, but if the

return is non-nil, it will be a string, which can be captured in a local variable and

subjected to any of the usual string manipulations, including regular-expression searches,


One Way to Use Response Dialogs

Here’s a tip that should be of help if you’re new to JavaScript. One neat use for

response dialogs is as debugging tools. If you write your code so that any error or strange

condition pops an alert, you can at least tell where you are in your code. Likewise, if

you’re ever unsure what kind of object you’re dealing with, stuff a variable’s name into the

alert dialog’s main argument and see what prints out at runtime.

You can even use a response dialog to modify variables, or control flow, at runtime. If

you’ve got a function that you need to test thoroughly, set it up so that you can input

values dynamically via a response dialog.

You can even enter code into the response dialog at runtime. Here’s an example

showing how:

x = 5;y= 8;str = app.response('Enter some code.');func = new Function(str);

app.alert( func() );

If you run this code and (when the dialog comes up) enter ‘return x * y’, an alert will

come up with the value ’40’ displayed. If you had entered ‘return x/y’, the alert would come

up with ‘0.625’ showing.

If that’s still not clear, don’t worry. We’ll go over it in more detail another time.

You May Also Like

About the Author: Kas Thomas

Leave a Reply