Accessing JS functions from FileMaker
FileMaker Pro can reach inside the JavaScript in a web viewer to trigger a function to run.
However, FileMaker can’t reach deeply inside the browser to get a function. It can only reach so far into the code. The furthest FileMaker can reach is called the global scope. Inside this global scope is a global object.
I am not sure if it’s quite accurate, but I think of the browser as layers of some type of food–cake probably. The top layer, the frosting, is the global scope. FileMaker Pro can only get to the frosting.
We’re familiar with the global scope, right? A $$ variable or field with global storage is accessible everywhere in FileMaker. So too, is there a global scope in JS; if a function is on the global scope in the JS, FileMaker can access it.
In the browser, this global scope / object is called the ‘window’.
A function we write in JS is not necessarily on the window where FileMaker can reach it. We have to specifically put it on the window into the window object. And we do so by adding the ‘window.’ prefix to a function, putting the function into a variable on the window.
Here’s the function:
function alertUser() {
alert ("I'm alerting you");
}And here is the function being put on the window:
window.alertUserFM = alertUserI’m creating a variable ‘alertUserFM’ as a property of the window object and assigning it the function ‘alertUser’. The variable then ‘alertUserFM’ can be called by FileMaker.
(Note: I usually name the variable and the function the same. In this example, though, I wanted to show which one is the variable and which is the function.)
You could shortcut these lines by writing the following:
window.alertUserFM = function(){
alert("I'm alerting you")
}This defines the function and places it into a property of the window object.
In the JS in FM course I teach this idea as a natural part of what we do. We have to do this in the JS Dev Environment because of the modular-based structure of the environment. And that’s okay.
There’s another way to put a function in the global scope. We’ll look at that in another post later on. But what’s described above.