HTML5 - JavaScript functions: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
m (Added 'MapTool.' to function names; added getUserData() example)
Line 7: Line 7:
==== Functions ====
==== Functions ====


* {{code|getName()}} - Returns the name of the panel.  
* {{code|MapTool.getName()}} - Returns the name of the panel.
* {{code|getKind()}} - Returns the kind of the panel: {{func|frame5}} or {{func|dialog5}}.
 
* {{code|getuserData()}} - Returns the data passed to the panel via the {{code|value}} option.  
* {{code|MapTool.getKind()}} - Returns the kind of panel the JS is inside of: {{func|frame5}} or {{func|dialog5}}.
* {{code|log()}} - Anything passed to this method will be sent to the chat window, just like a {{func|broadcast}} to {{code|self}}.
 
* {{code|MapTool.getUserData()}} - Returns the data passed to the panel via the {{code|value}} option.
 
<syntaxhighlight language="mtmacro">
[html.frame5("Example Dialog",
            "lib://myAddon/index.html",
            'value={"key": "some arbitrary data"}')]
</syntaxhighlight>
 
The variable {{code|value}} is injected into the JavaScript as a global variable (ie. as a property of the {{code|window}} object), such that it can be accessed as either {{code|window.value}} or just {{code|value}}.
 
<syntaxhighlight language="javascript">
// There's an implied assignment to 'value' here, prior to your JavaScript code
 
window.addEventListener("load", function() {
    console.log("Loaded");
    console.log(JSON.stringify(MapTool.getUserData()));
})
</syntaxhighlight>
 
* {{code|MapTool.log()}} - Anything passed to this method will be sent to the chat window, just like a {{func|broadcast}} to {{code|self}}.
 
Additionally, {{code|console.log()}} will be redirected to the chat window as well.  
Additionally, {{code|console.log()}} will be redirected to the chat window as well.  
[[Category:HTML5 JavaScript]]
[[Category:HTML5 JavaScript]]

Revision as of 11:06, 4 March 2024

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

A special class is available to JavaScript code running inside HTML5 pages. It allows access to a few values and functions.

The class itself is static and is called MapTool.

Functions

  • MapTool.getName() - Returns the name of the panel.
  • MapTool.getKind() - Returns the kind of panel the JS is inside of: frame5() or dialog5().
  • MapTool.getUserData() - Returns the data passed to the panel via the value option.
[html.frame5("Example Dialog",
             "lib://myAddon/index.html",
             'value={"key": "some arbitrary data"}')]

The variable value is injected into the JavaScript as a global variable (ie. as a property of the window object), such that it can be accessed as either window.value or just value.

// There's an implied assignment to 'value' here, prior to your JavaScript code

window.addEventListener("load", function() {
    console.log("Loaded");
    console.log(JSON.stringify(MapTool.getUserData()));
})
  • MapTool.log() - Anything passed to this method will be sent to the chat window, just like a broadcast() to self.

Additionally, console.log() will be redirected to the chat window as well.