Monday, October 10, 2011

Hello Again

Simple though it may be, the Hello World code has one cryptic aspect: why the second parameter with the value "1"? The comment tells part of the tale. But it omits the fact that, in this case, Cancel is the default option.

Alert("Hello World!", 1); /* FM Alert With OK and Cancel buttons */



If I change the parameter to "0", I get an alert box with OK and Cancel buttons with OK as the default.



Consulting the documentation (page 649-650 of the Scripting Guide) reveals that the first parameter is the message to display and the second determines the type of message box uses. A less cryptic version of Adobe's Hello World is shown here using the appropriate constant value:

Alert("Hello World!", Constants.FF_ALERT_CANCEL_DEFAULT);

My own Hello World might better be written:

Alert("Hello World!", Constants.FF_ALERT_OK_DEFAULT);

There are actually six different alert box types, each with a corresponding constant. The number is easier to type but the constant, once learned, will make your code clearer and more maintainable. 

By the way, the Alert() method returns 0, if the user clicks OK and -1 otherwise.  

No comments:

Post a Comment