If you’re anything like me, you probably love exploring the endless possibilities that JavaScript has brought to FileMaker, especially with the introduction of the “Perform JavaScript in Web Viewer” script step back in FileMaker Pro 19.
This feature significantly expanded the capabilities of FileMaker solutions and finally allowed direct interaction with JavaScript from within a Web Viewer. I found myself using it quite a lot in my solutions.
One annoying drawback with the “Perform JavaScript in Web Viewer” script step is that it only allows you to run a single line of JavaScript at a time. But for more complex tasks, I found I often needed multiple script steps, one for each line of JavaScript. This quickly became quite tedious.
If you have access to the code you’re interacting with in the Web Viewer, this isn’t so much of an issue. You could simply move your code into a new function within the external JavaScript and call it via a single script step in FileMaker.
However, if you’re interacting with a third-party website, you most likely don’t have the luxury of modifying their code, so running your code within FileMaker is your only choice.
For example, we built a solution for a client that logs into their banking website (which didn’t offer an API) and extracts transactions to process in FileMaker. The process involved multiple steps such as waiting for pages to load, clicking elements, and writing to a log file. Writing and debugging all this with multiple script steps in FileMaker would have been tedious.
The solution is actually quite simple.
Instead of running each line of JavaScript separately, you can bundle your code into a single variable and then call the “eval” JavaScript function, passing your script variable as a parameter. That’s it!
Example code:
Set Variable [$jsCode, "
// line 1 of js code;
// line 2 of js code;
// line 3 of js code;
// line 4 of js code;
"]
Perform JavaScript in Web Viewer [Object Name: “webViewer”, Function Name: “eval”, Parameters: $jsCode]
Now, you’re ready to run multiple lines of JavaScript code within a single “Perform JavaScript in Web Viewer” script step.
The JavaScript eval function simply executes the string variable as code. This is similar to FileMaker’s Evaluate() function if you’ve ever used it.
As a bonus, since I prefer to write my JavaScript outside of FileMaker because it’s more convenient and easier to debug, having the JavaScript in a single variable allows me to easily copy and paste my entire code without needing to break it out into multiple FileMaker script steps.
This simple trick has made working with complex JavaScript in FileMaker much easier. It saves you from using multiple FileMaker script steps for each line of JavaScript. Your scripts are much easier to read and maintain. Best of all, you can copy and paste your code all in one go.
Visit the FileMaker documentation for further information on the Perform JavaScript in Web Viewer script step.
If you need help working with JavaScript within FileMaker, drop us a line via our Contact Us page. We’ll be happy to assist.
Cheers,
Tony Tanevski