When should you use a Script Object versus a Script action?
Script Objects are mainly intended to hold self-contained functions which are then called by Script Actions. It is not recommended practice to write script actions into a Script Object as you cannot control properly when the script will be run. In particular you should note that script in Script Objects will not be run in Flash.
-Write a Script Object when a function is required by more than one object on a page. For each object that requires the function, call the function from a Script action. Any functions written in a Script Object are available to any Script action on the page.
-Write a Script action when you don’t need to declare any functions. Functions cannot be declared in a Script action.
-Write a Script action when the script must be triggered by something, such as a button being clicked. If you have a large amount of code, declare a function in a Script Object on the page and then call the function from the Script action.
Let’s use an example to explain this concept - imagine a publication that provides a Currency Conversion Calculator that calculates the conversion rate for six different countries. The following solution will show one way of creating the Currency Conversion Calculator that combines the two methods of adding an OpusScript to a publication.
Create one Script Object for the page. Write the six functions that convert the amount entered by the user to the local currency e.g. function FrenchConverion(); function ItalianConversion() etc.
Create seven buttons on the page and add a Script action to each - use Left Mouse Click as the Trigger for the Script Action.
For the first six buttons call one of the functions you have declared in the Script Object e.g. for the first button call the French conversion function i.e. FrenchConversion(); for the second button call ItalianConversion().
For the seventh button, call all six functions.
In this example, the Script Object holds all the calculations and the Script actions simply call the same Script Object. The Currency Conversion Calculator could have been created using only Script actions; the conversion calculation for each country could have been written in Script actions on the buttons. However, the seventh button would have had to repeat the code on the other buttons.
Tip:
Add one Script Object to a page. In the Script Object, add all the calculations and functions required for that page. Call the required functions in the Script Object using Script actions.
Advanced Tip:
Functions in a Script Object are only available to other objects on the current page and not to other pages or other objects on other pages within your publication. If the same functions are required on multiple pages within your publication, create one External Script that contains all the functions for your publication and import the External Script as a Script Object to every page that requires it – see External Script for more information.