Here's a cool trick for running JavaScript functions from FileMaker scripts and retrieving the result.
Create a FM script called Exit With Parameters that has just one line: Exit Script [ Get ( ScriptParameter ) ]
In the webviewer code, create a function that contains FileMaker.PerformScript ( 'Exit With Parameters', dataYouWantToReturn )
Within your FM script do Perform JavaScript in Web Viewer [ "jsFunctionName" ; dataYouWishToProcess ] , and right after that, do Get ( ScriptResult ) to retrieve the value returned by the JS function.
The cool thing is, Perform Javascript in Web Viewer waits for the JS function to complete. The JS function runs a FM script that sets the contents of ScriptResult, which you're now free to use in the rest of your script!
NOTE: based on my tests, the JS will run synchronously, that is FM will wait for the function to finish, even within Web Direct (unless my test was flawed). However, if you write anything in the JavaScript that causes the function to run asynchronously, FM will not wait for the result.
This is a nice, simple example. Still trying to get my head around the full range of possibilities with JavaScript, particularly with respect to the new functionality in FM19, so this is great to be able to debug and play around with.
Out of interest - where did you learn Javascript? Any specific books / online courses? Or just through working with it?
Here's an example I did for a posting on the regular forum where people were posting long "LET" FMP examples to do what you can do in JS in a single line of code.
This pattern sends the JavaScript to a micro-service where, using the Nashorn library (in Java), the Java micro-service will run JavaScript.
Note: Nashorn has been deprecated in current Java versions in favor of GraalVM.
https://www.graalvm.org/getting-started/
(A standalone Java Development Kit to execute Java or JVM-based languages (e.g., Scala, Kotlin), dynamic languages (e.g., JavaScript, R, Ruby, R, Python), WebAssembly, LLVM-based languages (e.g., C and C++) -- all FROM Java! )
JS is easy to learn, just get a good IDE like Webstorm and start coding a bit. It all comes when you have a particular need. TONS of examples online. W3Schools is good also.
Running ad-hoc JavaScript is just another method in my existing FMP micro-service (out of about 150 other methods to do whatever).
I’m very much still learning Javascript, it’s a wacky language. I’m loving it it though, especially all the async stuff. I’ve watched some LinkedIn learning and YouTube tutorials, but mostly I just tinker around and look stuff up as I get stuck.
I just realized I left a bunch of cruft in the JS in the original example file. If you downloaded it already, please replace the HTML field contents with the following. (I've also replaced the example file above).