Use custom Javascript
Custom JavaScript can be provided to use in component properties. This can be done in two ways:
- Add static JavaScript files.
- Use
Form
methodaddCustomJavaScript
.
JavaScript Files
Create a js
folder in the same (package) folder your gui_init
and gui_event
functions are in.
All files with .js
extension will be added automatically.
The functions and variables defined in the Javascript files will be available for global use in custom validators, callbacks, etc.
addCustomJavaScript method
The Form
static method addCustomJavaScript
can be used to programmatically add custom JavaScript. This may be convenient when developing reusable components that need some custom JavaScript to be available when they are used.
The custom JavaScript is added in a <script>
element in the header of the form.
Optionally, attributes for the <script>
can be specified as a dictionary, where the values must be (convertible to) strings. For attributes that do not have a value, use None
/missing
.
A special case it the src
attribute, that can be used for externally hosted scripts1. In that case the script contents can be left empty.
Form.addCustomJavaScript("alert('Hello, world!');")
Form.addCustomJavaScript("", attributes={"src": "https://example.com/js/lib.js"})
Form.addCustomJavaScript("alert('Hello, world!');");
Form.addCustomJavaScript(...
"", ...
"Attributes", struct("src", "https://example.com/css/styles.css"));
Note that cross-site protections may restrict access to certain domains, e.g.: MATLAB's uihtml restricts access to local files and some MathWorks domains. Discuss with your Simian Portal admin for options when deploying apps.