How to setup Webviewer in Filemaker to use JavaScript
This tutorial will guide you through the process of setting up a Web Viewer in FileMaker to use JavaScript, allowing for more interactive and dynamic web content within your FileMaker solution.
Step-by-Step Guide
Step 1: Create a New FileMaker File
- Open FileMaker Pro.
- Go to File > New Solution.
- Name your file (e.g.,
WebViewerJS) and choose a location to save it. - Click Save. The database window will pop up.
Step 2: Create an HTML Field
- In the database window, go to File > Manage > Database.
- In the Fields tab, create a new text field named
HTMLContent. - Click OK to save the field.
Step 3: Add the HTML Template to the HTML Field
- In Browse mode, select the layout that was automatically created (e.g.,
Main Layout). - Enter data mode (click on the pencil icon in the status toolbar).
- Click into the
HTMLContentfield and enter the following HTML template with embedded JavaScript:
<html>
<head>
<title>FileMaker Web Viewer</title>
<script>
function sendToFM() {
FileMaker.PerformScript("ReceiveFromJS", "Hello from JavaScript!");
}
function receiveFromFM(message) {
document.getElementById('message').innerText = message;
}
</script>
</head>
<body>
<h1>FileMaker Web Viewer with JavaScript</h1>
<button onclick="sendToFM()">Click Me</button>
<p id="message"></p>
</body>
</html>
Step 4: Add a Web Viewer to the Layout
- Switch to Layout mode.
- Select the Web Viewer tool from the toolbar.
- Draw a rectangle on the layout where you want the Web Viewer to appear.
- The Web Viewer Setup dialog will appear.
Step 5: Configure the Web Viewer to Use the HTML Field
- In the Web Viewer Setup dialog, choose Specify Custom Web Address.
-
Enter the following formula to reference the
HTMLContentfield:"data:text/html," & YourTable::HTMLContent - Ensure that Allow JavaScript to perform FileMaker scripts is enabled in the same window.
- Name the Web Viewer object
WebViewerin the Object Name field. - Click OK to close the dialog.
Step 6: Create a Script to Handle JavaScript Calls
- Go to Scripts > Script Workspace.
- Create a new script named
ReceiveFromJS. -
Add the following steps to the script:
Show Custom Dialog ["Message from JavaScript"; Get(ScriptParameter)] - Save and close the script.
Step 7: Test the Integration
- Switch to Browse mode.
- Click the button in the Web Viewer.
- You should see a dialog box with the message "Hello from JavaScript!".
Step 8: Additional Enhancements
Passing Data from FileMaker to JavaScript
- Create a field (e.g.,
Message) and place it on the layout. -
Modify the HTML template in the
HTMLContentfield to include the field data:<html> <head> <title>FileMaker Web Viewer</title> <script> function sendToFM() { FileMaker.PerformScript("ReceiveFromJS", "Hello from JavaScript!"); } function receiveFromFM(message) { document.getElementById('message').innerText = message; } </script> </head> <body> <h1>FileMaker Web Viewer with JavaScript</h1> <button onclick="sendToFM()">Click Me</button> <p id="message"></p> </body> </html> -
Add a new script in FileMaker to call the
receiveFromFMfunction in JavaScript:Perform JavaScript in Web Viewer [ Object Name: "WebViewer"; Function Name: "receiveFromFM"; Parameters: YourTable::Message ] - Trigger this script whenever you want to pass data from FileMaker to the Web Viewer.
By following these steps, you will set up a new FileMaker file that can communicate with JavaScript in a Web Viewer, allowing for more interactive and dynamic web content within your FileMaker solution.