JavaScript language

JavaScript is used by VRML to add more interactivity in VRML scene.
More information about JavaScript language: http://en.wikipedia.org/wiki/Javascript.


JavaScript is supported by VRML via the Script node.
The url property contains JavaScript code.
Script node interface:


Example:


Note:
JavaScript code starts by javascript: (but it is not required).

Warning:
Don't use " (quotation mark) character in JavaScript part (functions, comments, ...).
This character is used to close the url property of Script node.
Example:
Browser.print("hello world"); // Syntax error
Browser.print('hello world'); // Valid code


Fields are used to:


Field declaration syntax:

<type>:
Field type (SFString, MFString, SFInt32, MFInt32, SFFloat, MFFloat, SFBool, SFTime, ...).

Note:
Use SFNode to link a node with a script variable.

<name>:
Field name. This name is used in JavaScript.

<default_value>:
Default field value. Default value is not required.

<node_name>:
Node name (defined by DEF <node name> in VRML scene).
To use a VRML node in JavaScript function.


Example:


Comments

MeMo use // to introduce a line of comment.

Warning:
Multi-line comments are not supported by MeMo.

Note:
Comments are removed during M4M compilation. Feel free to comment your code!

Comments syntax:


Functions

MeMo use the same syntax than JavaScript to declare and use a function.
Use return keyword to return a value.
Function declaration syntax:


Example:


initialize() function

initialize() function is called when scene is loaded.
We use this function to initialize VRML scene.
Example:


Local variables

Variables are declared only in function (local variable).
We cannot declare global variable (use field if global variables are required).
Function declaration syntax:


Process events in JavaScript

VRML use ROUTE keyword to manage events.
We can use JavaScript to send an event or receive and process this event.
Events declaration syntax is same than field declarations. We use eventIn and eventOut keyword to replace field keyword.

To receive and process an event:


Example:


To send an event:


Example:


JavaScript libraries

To advanced use, we can use JavaScript libraries.
See JavaScript reference chapter.


Class declarations

MeMo doesn't support class declaration.
We cannot describe and use personal classes.
Solution: Use a VRML prototype.

Create a node with JavaScript

We cannot create VRML nodes dynamically with MeMo.


Other

More:
See FAQ for more restrictions.