How to use JavaScript Libraries in Designer

I have added a custom JavaScript file in the designer. How do I use the functions in the scripts section?

Hello Andrew,

You cannot use JavaScript modules and files from the Scripts, since JavaScript files are exclusively front-end (running in the browser or, in the case of print, internally before the PDF is produced).

Scripts, on the other hand, are only run locally in the document and never available to the front-end. So they are 2 completely separate scripting systems.

In some cases it might be possible to take code running in the browser and use it in Scripts, but that code would have to be inside of a Script in the Scripts pane.

Hi Evie,

Well my hope was to be able to create and use custom functions, much like the Formatter.date() funcitons. So you’re saying that it is not possible to add a JavaScript file and use those funcitons in the Scripts section?

Not in the JavaScript folder. However, you may add new functions in the Scripts pane.

You probably want to make functions that will be available in all the scripts, however. And this is possible using a bit of a cheat, taking advantage of “variable hoisting” in JavaScript.

Basically, if you declare a variable, or a function, without using the “var” keyword before it, it’ll be available globally. So for example if I want a function that adds 2 numbers:

addNums = function(a, b) { return a + b; }

This function will be available everywhere in all subsequent scripts (remember scripts are evaluated top to bottom, so this should be one of the top ones).

Hope this helps!

That will work, I was able to declare my custom functions in the first script and use throughout the remaining scripts where needed. Thank you for the help!