Skip to main content
Known Participant
September 9, 2022
Question

Forms: Javascript Best Practices - writing scripts for each field vs one script, i.e. hidden calc...

  • September 9, 2022
  • 3 replies
  • 1987 views

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.

 

 

This topic has been closed for replies.

3 replies

JR Boulay
Community Expert
Community Expert
October 20, 2022

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 Photoshopographe
dankpoochAuthor
Known Participant
October 20, 2022

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.

Bernd Alheit
Community Expert
Community Expert
October 20, 2022

You can declare variables at document level.

Or use hidden form fields with the values. 

try67
Community Expert
Community Expert
September 10, 2022

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.

JR Boulay
Community Expert
Community Expert
September 10, 2022

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 Photoshopographe
dankpoochAuthor
Known Participant
October 19, 2022

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).