runJsFunction: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Conversion script moved page runJsFunction to RunJsFunction without leaving a redirect: Converting page title to first-letter uppercase)
m (Conversion script moved page RunJsFunction to runJsFunction: Converting page titles to lowercase)
(No difference)

Revision as of 23:42, 9 February 2023

 This article describes a feature or macro function that is experimental and may be subject to change.

runJSfunction() Function

Introduced in version 1.8.0
Runs a JavaScript function which is defined in a frame5, dialog5, or overlay.

Usage

runJSfunction(name, type, func, thisArg, argsArray)

Parameter

  • name - Name of an active frame, dialog, or overlay.
  • type - Either "frame", "dialog", or "overlay". This is needed because a frame, a dialog and an overlay could all share the same name, in which case it is not clear which component is the target.
  • func - Name of the JavaScript function to run.
  • thisArg - Value of "this" provided to the function. Should be "null" or the name of a variable already defined in the script.
  • argsArray - JSON array specifying the arguments with which the JavaScript function should be called.

Examples

Changing info displayed on the frame by passing a JSON argsArray to the function changeHP.
[frame5("Test 1"):{
<script>
[r: "function changeHP(hp) { document.getElementById('hp').innerHTML = hp;}"]
</script>
Hitpoints: <span id="hp">10</span>
}]

Call the changeHP Javascript function.

[r: runJsFunction("Test 1", "frame", "changeHP", "null", json.append("[]", "3"))]

Using thisArg in call to alertFullName.

[frame5("Test 2"):{
<script>
[r: 'function alertFullName() { alert("Full Name: " + this.firstName + " " + this.lastName);}']
var person = [r: json.set("{}", "firstName", "John", "lastName", "Smith")];
document.write("<b>First Name:</b> " + person.firstName + "<br/><b>Last Name</b>: " + person.lastName + "<br/>");
</script>
}]

Call alertFullName and specify the Javascript variable person.

[r: runJsFunction("Test 2", "frame", "alertFullName", "person")]