US: 800.995.PLUM UK: 0845.355.3330
US: 800.995.PLUM UK: 0845.355.3330
from the Plum Voice IVR Glossary
JavaScript is a dynamic, weakly typed, prototype-based language with first-class functions that is in widespread use within web browsers and other client-side programmable environments. More formally, it is a dialect of ECMAScript, the most widely supported version being JavaScript 1.5 which parallels the ECMA-262 standard (3rd edition, PDF).
JavaScript is unrelated to the Java programming language. It was developed at Netscape and first released as part of the Netscape web browser in 1995. Since then, it has seen widespread uptake amongst web browsers from all vendors, and is now a near-universal internet technology. Its popularity spread to other uses as a scripting language for applications such as Adobe's Creative Suite and OpenOffice.org, and its close variant ActionScript serves as the scripting runtime for Shockwave and Flash web-embeddable movies. ECMAScript/JavaScript is the scripting language supported by the VoiceXML standard.
JavaScript's syntax is similar to C and Java in that it uses curly braces, but unlike them, it is an interpreted language, so it does not need to be compiled, and is well suited for embedding within documents like VoiceXML and HTML. It is dynamically and weakly typed, meaning that the type of variables is determined at runtime, and variable types are automatically converted to suit the operations being performed. Also unlike C and Java, its method of inheritance is prototype-based, meaning its objects inherit methods and properties from a "prototype" object, instead of a class. This allows greater flexibility in changing object characteristics and inheritance patterns while a script is running, although it is a common stumbling block for programmers from backgrounds of classical object-oriented inheritance.
There are many implementations of JavaScript, with significant recent advancements in the speed of interpretation and execution using techniques such as Just-In-Time compilation. An implementation of JavaScript is usually called a JavaScript engine. The engine provides the environment that scripts will run in, such as a global object and associated objects with bindings to a browser or application, so that scripts can manipulate objects outside the scope of the engine itself. For example, every variable or field in VoiceXML is exposed as an JavaScript variable which can be manipulated freely within <script/> blocks or other JavaScript executable contexts. For HTML in a web browser, a binding to the layout engine is commonly implemented according to the W3C Document Object Model (DOM).
The following is an example of JavaScript being used in an HTML context, utilizing DOM functions such as document.createElement:
function isDocumentElementHeightOff(){
var d = document,
div = d.createElement('div');
div.style.height = "2500px";
d.body.insertBefore(div, d.body.firstChild);
var r = d.documentElement.clientHeight > 2400;
d.body.removeChild(div);
return r;
}
and the following is an example of JavaScript used in a VoiceXML context. Note that in VoiceXML there are no implicit global functions to manipulate the environment, as with the DOM in HTML. In VoiceXML, certain elements form JavaScript scopes, and other elements create and assign variables accessible to JavaScript. This is a VoiceXML document demonstrating the relationship between a few such elements.
<vxml version="2.0">
<form>
<var name="test_var"/>
<field name="cost" type="digits">
<prompt>
Say or enter a number to be used as a subtotal.
</prompt>
<filled>
<var name="taxrate" expr="0.08"/>
<var name="total"/>
<script>
//<![CDATA[
// The <field> entered above, "cost", is available as a variable here
// Also, the two <var> elements become JavaScript variables available here
cost = parseInt(cost, 10);
var tax = cost * taxrate;
total = tax + cost;
//]]>
</script>
<if cond="total > 1000">
<prompt>With tax, that total would be greater than one grand.</prompt>
<else/>
<!-- Likewise, the tax declared as a var inside the <script>
is available here for use in a <value expr=""/> -->
<prompt>
Your total with tax would be <value expr="total"/>.
The tax part was <value expr="tax"/>.
</prompt>
</if>
</filled>
</field>
</form>
</vxml>
For more information, see:
| © 2010 The Plum Group, Inc. |