Introduction to VRML 97
Using the JavaScript examples

These tutorial notes include several VRML worlds that use JavaScript program scripts within Script nodes. The text for these program scripts is included directly within the Script node within the VRML file.

JavaScript support

The VRML 97 specification does not require that a VRML browser support the use of JavaScript to create program scripts for Script nodes. Fortunately, most VRML browsers do support JavaScript program scripts, though you should check your VRML browser's release notes to be sure it is JavaScript-enabled.

Some VRML browsers, particularly those from Cosmo Software (Silicon Graphics), support a derivative of JavaScript called VRMLscript. The language is essentially identical to JavaScript. Because of Cosmo Software's strength in the VRML market, most VRML browser vendors have modified their VRML browsers to support VRMLscript as well as JavaScript.

JavaScript and VRMLscript program scripts are included as text within the url field of a Script node. To indicate the program script's language, the field value starts with either "javascript:" for JavaScript, or "vrmlscript:" for VRMLscript, like this:

Script {
    field SFFloat bounceHeight 1.0
    eventIn SFFloat set_fraction
    eventOut SFVec3f value_changed

    url "vrmlscript:
        function set_fraction( frac, tm ) {
            y = 4.0 * bounceHeight * frac * (1.0 - frac);
            value_changed[0] = 0.0;
            value_changed[1] = y;
            value_changed[2] = 0.0;
        }"
}

For compatibility with Cosmo Software VRML browsers, all JavaScript program script examples in these notes are tagged as "vrmlscript:", like the above example. If you have a VRML browser that does not support VRMLscript, but does support JavaScript, then you can convert the examples to JavaScript simply by changing the tag "vrmlscript:" to "javascript:" like this:

Script {
    field SFFloat bounceHeight 1.0
    eventIn SFFloat set_fraction
    eventOut SFVec3f value_changed

    url "javascript:
        function set_fraction( frac, tm ) {
            y = 4.0 * bounceHeight * frac * (1.0 - frac);
            value_changed[0] = 0.0;
            value_changed[1] = y;
            value_changed[2] = 0.0;
        }"
}

What if my VRML browser doesn't support JavaScript?

If your VRML browser doesn't support JavaScript or VRMLscript, then those worlds that use these languages will produce an error when loaded into your VRML browser. This is unfortunate since JavaScript or VRMLscript is an essential feature that all VRML browsers should support. Perhaps you should consider getting a different VRML browser...

If you can't get another VRML browser right now, there are only a few VRML worlds in these tutorial notes that you will not be able to view. Those worlds are contained as examples in the following tutorial sections:

  • Introducing script use
  • Writing program scripts with JavaScript
  • Creating new node types

So, if you don't have a VRML browser with JavaScript or VRMLscript support, just skip the above sections and everything will be fine.