Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
Sep 09, 2022 Sep 09, 2022

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.

 

 

TOPICS
JavaScript , PDF forms
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2022 Sep 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 Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 19, 2022 Oct 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). 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 10, 2022 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2022 Oct 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 Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 20, 2022 Oct 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2022 Oct 20, 2022

You can declare variables at document level.

Or use hidden form fields with the values. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 20, 2022 Oct 20, 2022

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2022 Oct 20, 2022

Does you execute functions at document level? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 26, 2022 Oct 26, 2022

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?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2022 Oct 26, 2022

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 26, 2022 Oct 26, 2022
LATEST

Same basic file you've seen. If you remove the hidden "Variables" textfield, most/all functionality disappears. Add buttons don't work, etc.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines