Forms: Javascript Best Practices - writing scripts for each field vs one script, i.e. hidden calc...
Copy link to clipboard
Copied
Hey all, I'm still relatively new to javascript for forms in acrobat. But my forms have quickly become quite complex. I design them in Indesign with as much functionality as possible and then add java in acrobat. I have fields with onBlur and onFocus actions, validations that display or hide other fields or buttons, dropdowns coded to hide or display other fields, indesign buttons that function as error messages that hide/display when needed.
The problem is: these scripts are all in separate places. I can edit them to an extent in the All JavaScripts editor. Honestly, that editor is more confusing to me at this point--I believe, due to the XML tags.
1. Is it best-practices to consolidate all scripts in one place, especially if I plan to make more complex forms?
2. Is that place the All JavaScripts editor or somewhere else?
Thanks for you time.
Copy link to clipboard
Copied
1. Yes.
2. I use to place all my scripts as functions in a document level script, then all fields call those functions. Which greatly simplifies maintenance and correction.
See: https://www.pdfscripting.com/public/Document-Level-Scripts.cfm
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
Moving all workflow to this. Love it. Maybe weird question: where do you put your variables? I thought I could just pop them up there alongside functions. But it appears they aren't always loaded/called properly? I'd appreciate if you could spell out a little more of how this should look for optimal efficiency (for a noob).
Copy link to clipboard
Copied
Do NOT use the Edit All JavaScripts command to edit your code. If something goes wrong, like an XML tag is broken, it can seriously screw up your entire file. Use the Document JavaScripts command to edit the doc-level scripts, instead.
Copy link to clipboard
Copied
A variable declared in a function can only be used in this function because it does not exist outside.
For a variable to be really available everywhere it must be declared outside (and before) any function.
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
Where do you put variables? Can they all be declared in the document-level (& how?) ? Should they be declared in each field in which they are used?
In my limited experience, it usually suffices to have all variables declared in one field. But I imagine this is not best-practice, since different field actions are triggered differently. I'm just going on trial and error here. But I'd appreciate a recommendation--especially one (if possible) that centralizes variables! Thanks again for this education.
Copy link to clipboard
Copied
You can declare variables at document level.
Or use hidden form fields with the values.
Copy link to clipboard
Copied
Thank you. I've had a problem arise when using one large doc level script to store all my variables and functions. For some reason, some such variables, sometimes, came up as undefined. In troubleshooting, I read "care must be taken not to run code in one of these scripts that depends on the document being loaded" and theorized I may be declaring variables improperly. I read from you all that declaring variables in doc-level and elsewhere is proper practice and my error must be somewhere else.
https://www.pdfscripting.com/public/Document-Level-Scripts.cfm
Copy link to clipboard
Copied
Does you execute functions at document level?
Copy link to clipboard
Copied
I execute functions at the document level. I also created a Document JavaScript called "1. Field Variables". Inside it contains no 'function', only variable definitions (i.e. "var add1 = this.getField("Add 1"). My thinking was that all such common variables would be reliably defined prior to and outside of all other functions, thereby universally 'callable' within the document. This does not appear to be the case. These variables, at least sometimes, are not being found by other functions.
What is best, then? 1. Define variables within each doc level function? 2. Define all in a hidden field within the doc? Something else?
Copy link to clipboard
Copied
It should work. Could you share the file in question?
A more globally available type of variable is one that's defined as a part of the global object. Those variables will be accessible across all scripts in your file, and even between files. They can even be made to be persistent, so they retain their values between sessions of the application.
Copy link to clipboard
Copied
Same basic file you've seen. If you remove the hidden "Variables" textfield, most/all functionality disappears. Add buttons don't work, etc.

