runJsFunction: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Added examples. Added missing func param.)
m (Minor corrections)
Line 12: Line 12:


'''Parameter'''
'''Parameter'''
{{param|name |Name of an active frame, dialog, or overlay.}}
{{param|name|Name of an active frame, dialog, or overlay.}}
{{param|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 not clear which component is the target.}}
{{param|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.}}
{{param|func|Name of the Javascript function to run.}}
{{param|func|Name of the JavaScript function to run.}}
{{param|thisArg|Value of "this" provided to the function. Should be "null" or the name of a variable already defined in the script.}}
{{param|thisArg|Value of "this" provided to the function. Should be "null" or the name of a variable already defined in the script.}}
{{param|argsArray|JSON array specifying the arguments with which the JavaScript function should be called.}}
{{param|argsArray|JSON array specifying the arguments with which the JavaScript function should be called.}}

Revision as of 04:08, 4 May 2021

 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")]