Contents of this Section:
A scripting language is an easy-to-use programming language that is used to manipulate, customize, and automate the facilities of an existing system, such as a Web browser or the Jargon Reader deployment engine.
In such systems, useful functionality is already available through a graphical user interface (GUI), and the scripting language is a way to control that functionality. In this way, the existing system is said to provide an environment of objects and facilities which completes the capabilities of the scripting language.
A scripting language is designed for both professional and non-professional programmers, and so it normally has a number of informalities built into the language.
ECMAScript is a standard Internet scripting language formally known as ECMA-262 (ECMAScript), sanctioned by the European Computer Manufacturers Association (ECMA). The language spec was based on an early version of JavaScript. This specification was then used by Microsoft, Netscape, Opera, and other browser venders who have provided support for it in later JavaScript and JScript versions.
ECMAScript is an interpreted, object-based scripting language. Although it has fewer capabilities than full-fledged object-oriented languages like C++, JScript is more than sufficiently powerful for its intended purposes. ECMAScript is not a cut-down version of another language (it is only distantly and indirectly related to Java, for example), nor is it a simplification of anything.
JavaScript and JScript include the core ECMAScript language plus many extensions that deal specifically with browser objects such as document, window, frame, form, etc. The methods for these browser-related objects can be ignored if you look at JavaScript documentation, since the scripting engine is being run within the context of the Jargon Reader engine, not within the context of a web browser. Only the core ECMAScript language features are used in Jargon Reader, not JavaScript or JScript extensions.
The scripting engine used by Jargon Software supports primarily the core ECMAScript language features, plus a few other useful methods from JavaScript and/or JScript that are generic methods (not related to browser objects). This scripting has been extended by Jargon Software to provide support for methods used by graphical components, host tasks, and other features of the Jargon Reader run-time environment.
ECMAScript is object-based: basic language and host facilities are provided by objects, and an ECMAScript program is a cluster of communicating objects. An ECMAScript object is an unordered collection of properties each with 0 or more attributes which determine how each property can be used.
Properties are containers that hold other objects, primitive values, or methods. A primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, and String; an object is a member of the remaining built-in type Object; and a method is a set of programming statements associated with an object via a property.
ECMAScript defines a collection of built-in objects which round out the definition of ECMAScript entities. These built-in objects include the Global object, the Object object, the Function object, the Array object, the String object, the Boolean object, the Number object, the Math object, and the Date object.
ECMAScript also defines a set of built-in operators which may not be, strictly speaking, functions or methods. ECMAScript operators include various unary operations, multiplicative operators, additive operators, bitwise shift operators, relational operators, equality operators, binary bitwise operators, binary logical operators, assignment operators, and the comma operator.
ECMAScript syntax intentionally resembles Java syntax. ECMAScript syntax is relaxed to enable it to serve as an easy-to-use scripting language. For example, a variable is not required to have its type declared nor are types associated with properties, and defined functions are not required to have their declarations appear textually before calls to them.
ECMAScript is a loosely typed language. Loosely typed means you
do not have to declare the
data types of variables explicitly. In fact, you cannot explicitly
declare data types. Moreover, in many cases ECMAScript performs conversions
automatically when needed. For instance, if you add a number to an item
consisting of text (a string), the number is converted to text.
A function contains some code that will be executed by an event or a call to that function. A function is a set of statements. To create a function you define its function name, any calling parameters ("arguments"), and one or more statements to be executed. A function may not be declared within another function. All functions may be called from anywhere in a script.
Functions have the following structure:
A statement block is a group of statements enclosed in curly braces, "{}", which indicate that the enclosed individual statements are a group and are to be treated as one statement. A block can be used anywhere that a single statement can.
Here are some general guidelines for writing the statements within a function:
| \a | Audible bell |
| \b | Backspace |
| \f | Formfeed |
| \n | Newline |
| \r | Carriage return |
| \t | Horizontal Tab |
| \v | Vertical tab |
| \' | Single quote |
| \" | Double quote |
| \\ | Backslash character |
| \0 | Null character (e.g. "\0" is the null character) |
| \### | Octal number (0-7) (e.g. "\033" is the escape character) |
| \x## | Hex number (0-F) (e.g. "\x1B" is the escape character) |
| \u#### | Unicode number (0-F) (e.g. "\u001B" is the escape character) |
Reserved Words
The following words have special meaning for the interpreter and cannot
be used as identifiers for variable, component or function names:
A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value.
Rules for Variable names:
For example, both of the following statements are valid ways to create a variable and assign it a value.
Arrays
A special type of variable is an array, which is an instance of the
standard ECMAScript Array object. An Array is a special class of object
that refers to its properties with numbers rather than with variable names.
Properties of an Array object are called elements of the array. The number
used to identify an element is called an index in brackets which follows
an array name. Array indices must be either numbers or strings. Array elements
can be of any data type. The elements in an array do not all need to be
of the same type, and there is no limit to the number of elements an array
may have.
Arrays use square brackets to specify the element number within the set of array values, normally starting with [0] for the first. Arrays are dynamic, and any index, positive or negative, into an array is always valid. If an element of an array is referenced, then the script engine ensures that such an element exists. For example, if a statement in a script is:
Arrays can be defined in two ways, by specifying the number of elements, or by specifying a set of element values. Examples:
Sorting an Array
To sort the elements of an array, use the array.sort method. This is
a very flexible sort routine which will (by default) sort strings in alphabetical
order. However, you can optionally provide a compare function that the
sort method will use to compare each pair of elements as it is sorting,
to handle date and numeric values.
SYNTAX: array.sort([compareFunction])The comparison of elements is done based on the supplied compareFunction. If compareFunction is not supplied, then the elements are converted to strings and compared. Non-existent elements are always greater than any other element, and consequently are sorted to the end of the array. Undefined values are also always greater than any defined element, and appear at the end of the Array before any empty values. Once these two tests are performed, then the appropriate comparison
WHERE: compareFunction - identifier for a function which expects two parameters x and y,
and returns a negative value if x < y, zero if x = y, or a positive value if x > y.
RETURN: object - this Array object after being sorted.
DESCRIPTION: This method sorts the elements of the array. The sort is not necessarily stable (that is, elements which compare equal do not necessarily remain in their original order).
If a compare function is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:
function compareNumbers(a, b)EXAMPLE: Consider the following code, which sorts based on numerical values, rather than the default string comparison.
{
return a > b
}
function compare( x, y )
{
x = ToNumber(x);
y = ToNumber(y);
if( x < y )
return -1;
else if ( x == y )
return 0;
else
return 1;
}
var array = new Array( 3, undefined, "4", -1 );
array.sort(compare);
// The sorted array is "-1,3,4,,"
// Notice the undefined value at the end of the array.
undefined
If a variable is created or accessed with nothing assigned to it, it
is of type undefined. An undefined variable merely occupies space until
a value is assigned to it. When a variable is assigned a value, it is assigned
a type according to the value assigned. Though variables may be of type
undefined, there is no literal representation for undefined. Since an undefined
variable will cast to a boolean value as false, it can be tested as follows
(although other values such as zero or an empty string will also cast to
false so it is an ambiguous test):
var test;null
if (!test)
myMessage = "test is undefined or has some other value that casts to false";
NaN
The NaN type means "Not a Number". NaN is an acronym for the phrase.
However, NaN does not have a literal representation. To test for NaN, the
global function isNaN() must be used, for example:
Comments may be of two types: single-line or multi-line. In a single-line
comment, all text following a pair of slashes ("//") to the end of that
line is treated as a comment and ignored. In a multi-line comment, all
text between a starting "/*" and ending "*/" is treated as a comment and
ignored. Multi-line comments may not be nested within multi-line comments,
but single-line comments can exist within multi-line comments.
The standard ECMAScript statements include variable definitions and
control commands for looping and conditional execution. The CONDITION in
"if" and "while" tests can be anything that evaluates to a boolean value.
For an explanation of how various data values convert ("cast") to a Boolean
value, see the discussion in the Boolean Properties
and Methods section.
break |
Statement that terminates the current while or for loop and transfers program control to the statement following the terminated loop. |
continue |
Statement that terminates execution of the block of statements in a while or for loop, and continues execution of the loop with the next iteration. |
do...while |
Executes its statements until the test condition
evaluates to false. Statements are executed at least once.
Syntax:
STATEMENT_BLOCK} while (CONDITION); |
for |
Creates a loop that consists of three optional expressions, enclosed
in parentheses and separated by semicolons, followed by a block of statements
executed in the loop.
Syntax:
STATEMENT_BLOCK }
n += i myfunc(n) } |
if...else |
Statement that executes a set of statements
if a specified condition is true. If the condition is false, another set
of statements can be executed. Note that the "else" clause is optional.
Also, the statement blocks can in turn contain nested "if" statements,
to any level.
Syntax:
STATEMENT_BLOCK } [else { STATEMENT_BLOCK }]
n3++; n4--;} else { n5++; n6--;} |
return |
Statement that exits a function and optionally specifies the value to be returned from running it. |
switch |
Allows a program to evaluate an expression and attempt to match the
expression's value to a case label.
Syntax:
case LABEL: STATEMENT_BLOCK; break; case LABEL: STATEMENT_BLOCK; break; ... default : STATEMENT_BLOCK; }
case "B" : my_color = "Blue"; break; case "R" : my_color = "Red"; break: default : my_color = "Unknown"; } |
var |
Statement that declares a variable, optionally initializing it to a value. |
while |
Statement that creates a loop that evaluates
an expression, and if it is true, executes a block of statements. If the
condition is initially false, no statements will be executed (unlike "do...while").
Syntax:
STATEMENT_BLOCK } |
ECMASCript provides a rich set of operators for arithmetic,
string,
logical
and bit-level operations, value
assignments, value comparisons,
and other special operations.
The built-in String, Boolean, Number, Date and Global objects include a variety of standard properties and methods that may be used on literal or variables of the appropriate data type. There is also a full set of math functions for use in scientific or financial applications. The following tables show the most commonly used methods. For a complete list, refer to the complete ECMAScript or JavaScript documentation.
A string is an ordered series of characters. The most common use for
strings is to represent text. To indicate that text is a string, it is
enclosed in quotation marks. You can declare a string with single quotes
instead of double quotes. There is no difference between the two ways.
Note that a String is both a primitive data type and an object. For most
practical purposes, this distinction makes no difference.
| Property Name | Description |
| length | Returns the number of characters
in the target string
Example:
myLength = myString.length; // returns 5 (Note: no parentheses) |
| Method Name | Description |
| charAt
(position) |
Returns the character at the given
position
in a string, where position starts at zero for the first character.
Example:
myChar = myString.charAt(2); // returns "c" // get the last character in the string lastChar = string.charAt(string.length - 1); // "e" |
| indexOf
(string[, offset]) |
Searches and (if found) returns the
index number of the search string within the target string.
The search begins at offset if specified, otherwise the search begins
at the beginning of the string. If no match is found, "-1" is returned
instead.
Example:
myPos = myString.indexOf("cd"); // returns 2 myPos = myString.indexOf("CD"); // returns -1 (case!!) |
| lastIndexOf
(string[, offset]) |
Like above, but searches the string backwards, starting from the right end, so it finds the last occurrence of the search string. |
| replace
(pattern,repl_exp) |
The target string is searched using the regular expression pattern
defined by pattern. If a match is found, it is replaced by the substring
defined by repl_exp, and returns the original string with replacements
in it made according to pattern and repl_exp. The parameter
repl_exp
may be:
• a simple string • a string with special regular expression replacement elements in it • a function that returns a value that may be converted into a string A full explanation of the powerful features of regular expressions is beyond the scope of this document. See any unix scripting manual. Simple examples:
var str = "aa bb cc bb dd"; var pat = /b(b)/g; // the "(b)" is $1 below rtn = str.replace(pat, "zz"); // rtn = "aa zz cc zz dd" rtn = str.replace(pat, "B$1"); // rtn = "aa Bb cc Bb dd" |
| split
(delimiter) |
Spits a string into multiple substring
values according to the specified delimiter, and returns the split values
as an array which is typically assigned to an array variable, starting
with element [0] and on for as many elements as are found.
Example:
var word=message.split(" ") //word[0] contains "Now", word[1] contains "you", etc |
| substr
(start, length) |
Returns a substring starting at position start and including
the next number of characters specified by length. If start
is positive, the position is relative to the beginning of the string. If
start
is negative, the position is relative to the end of the string.
Examples:
str.substr(0, 5) // == "01234" str.substr(2, 5) // == "23456" str.substr(-4, 2) // == "56" |
| substring
(from, up_to) |
Returns a substring of a string starting
at position start and going to, but not including, position up_to.
Examples:
str.substring(0, 5) // == "01234" str.substring(2, 5) // == "234" str.substring(0, 10) // == "0123456789" |
| toLowerCase() | Returns a copy of a string with all of the letters changed to lower
case.
Example:
eecummingsname = myName.toLowerCase(); // john smith |
| toUpperCase() | Returns a copy of a string with all of the letters changed to upper
case.
Example:
ShoutingName = myName.toUpperCase(); // JOHN SMITH |
Boolean Properties and Methods
A Boolean data type may have only one of two possible values: false or true. Since values are automatically converted when appropriate, Booleans can be used as they are in languages such as C. Namely, false is zero, and true is non-zero. A script is more precise when it uses the actual values (false and true), but it will work using the concepts of zero and not zero. When a Boolean is used in a numeric context, it is converted to 0, if it is false, and 1, if it is true.
Conversely, when a non-Boolean data type is converted to a Boolean value (as in an "if" test), the value is converted as follows:
| Method Name | Description |
| new Boolean(value) | Returns a Boolean object with the parameter value converted to a boolean
value.
Example:
var isRocky = new Boolean( name == "Peter" ); // The Boolean object "isRocky" is now true. |
| toString() | Returns the string "true" or "false" according to the value of the
Boolean object.
Example:
var aa = new Boolean( nn == "Peter" ); // comparison returns true var bb = false; myMsg1 = aa.toString(); // "true" myMsg2 = bb.toString(); // "false" |
A Number data type may be an integer or a floating point (exponential) value.
Integers
Integers are whole numbers. Decimal integers, such as 1 or 10, are
the most common numbers encountered in daily life. There are three notations
for integers: decimal, hexadecimal, and octal.
Decimal notation is the way people write numbers in everyday life and uses base 10 digits from the set of 0-9. Examples are:
1, 10, 0, and 999Hexadecimal notation uses base 16 digits from the sets of 0-9, A-F, and a-f. These digits are preceded by 0x. Scripts are not case sensitive when it comes to hexadecimal numbers. Examples are:
var a = 101;
0x1, 0x01, 0x100, 0x1F, 0x1f, 0xABCDOctal notation uses base 8 digits from the set of 0-7. These digits are preceded by 0. Examples are:
var a = 0x1b2E;
00, 05, and 077Floating point
var a = 0143;
Decimal floats use the same digits as decimal integers but allow a period to indicate a fractional part. Examples are:
0.32, 1.44, and 99.44Scientific floats are often used in the scientific community for very large or small numbers. They use the same digits as decimals plus exponential notation. Scientific notation is sometimes referred to as exponential notation. Examples are:
var a = 100.55 + .45;
4.087e2, 4.087E2, 4.087e+2, and 4.087E-2
var a = 5.321e33 + 9.333e-2;
| Method Name | Description |
| toFixed(decimals) | Returns a string representation of this number in fixed-point notation.
This method returns a string containing the number represented in fixed-point
notation with decimals digits after the decimal point.
Example:
var dollarAmt = fraction.toFixed(2); // "3.33" |
| toString() | Returns a string representation of this number. This method converts
a number to a string using a standard format for numbers.
Example:
var s = n.toString(); |
Date Properties and Methods
ECMAScript shines in its ability to work with dates. All standard ECMAScript
date methods are supported, with the important exception that all month references
start at 1 (for January) instead of 0, so you do not have to constantly remember
to add 1 to the month value when using these methods.
To create a Date object which is set to the current date and time, use the new operator, as you would with any object.
var currentDate = new Date();There are several ways to create a Date object which is set to a date and time. The following lines all demonstrate ways to get and set dates and times.
var aDate = new Date(milliseconds);
var bDate = new Date(datestring);
var cDate = new Date(year, month, day);
var dDate = new Date(year, month, day, hours, minutes, seconds);
| Method Name | Description |
| getDate() | Returns the day of the month, as a number from 1 to 31, of a Date
object. The first day of a month is 1, and the last is 28, 29, 30, or 31. |
| getDay() | Returns the day of the week, as a number from 0 to 6, of a Date object.
Sunday is 0, and Saturday is 6. |
| getFullYear() | Returns the year, as a number with four digits, of a Date object. |
| getMonth() | Returns the month, as a number from 1 to 12, of a Date object. January
is 1, and December is 12. |
| toDateString() | Returns the Date portion of the current date as a string. This string is formatted to read "Month Day, Year", for example, "May 1, 2000". This method uses the local time, not UTC time. |
Global Properties and Methods
The properties and methods of the global object may be thought of as
global values and functions. The object identifier global is not
required when invoking a global method or function. Indeed, the object
name generally is not used.
| Method Name | Description |
| eval(expression) | Evaluates whatever is represented by the parameter expression. If expression
is not a string, it will be returned. For example, calling eval(5) returns
the value 5.
If expression is a string, the interpreter tries to interpret the string as if it were an ECMAScript statement and execute it. If successful, the method returns the last variable with which it was working, for example, the return variable. If the method is not successful, it returns the special value, undefined. |
| getArrayLength(array) | Returns the length (number of elements) of an array. This function
should be used with dynamically created arrays, that is, with arrays that
were not created using the new Array() operator and constructor.
When working with arrays created using the new Array() operator
and constructor, use the length property of the Array object.
Example:
arr_Len = getArrayLength(arr); // 4 |
| isNaN(value) | Returns a boolean value: true if value is not a number, else
false.
Example:
if (isNan(numValue)) return "Not a number" else return "OK"; } |
| parseInt(str[, radix]) | Returns the integer to which the target string converts, else NaN.
This method converts an alphanumeric string to an integer number. The first
parameter, str, is the string to be converted, and the second parameter,
radix,
is an optional number indicating which base to use for the number. If the
radix parameter is not supplied, the method defaults to base 10 which is
decimal. If the first digit of string is a zero, radix defaults to base
8 which is octal. If the first digit is zero followed by an "x", that is,
"0x", radix defaults to base 16 which is hexadecimal.
White space characters at the beginning of the string are ignored. The first non-white space character must be either a digit or a minus sign (-). All numeric characters following the string will be read, up to the first non-numeric character, and the result will be converted into a number, expressed in the base specified by the radix variable. All characters including and following the first non-numeric character are ignored. If the string is unable to be converted to a number, the special value NaN is returned. Example:
var i = parseInt("9.3"); // In both cases, i == 9 |
| parseFloat(str) | Returns the floating point number to which the string converts, else NaN. This method is similar to global.parseInt() except that it reads decimal numbers with fractional parts. In other words, the first period, ".", in the parameter string is considered to be a decimal point, and any following digits are the fractional part of the number. |
For details on other standard ECMASCript methods not covered here, such as methods for the Math object, consult any comprehensive JavaScript or JScript manual, or see any of the following Web sites:
Netscape JavaScript Reference:
http://developer.netscape.com/docs/manuals/communicator/jsref/index.htm
Microsoft JScript Reference:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsoriJScript.asp
Web Developers Virtual Library (WDVL) - Authoring JavaScript
http://wdvl.internet.com/Authoring/JavaScript/
DevGuru JavaScript Quick Reference
http://www.devguru.com/Technologies/ecmascript/quickref/javascript_index.html
Using ODBC with Local SQL Databases
Scripts can now use ODBC connections and SQL statements to read and update local databases, such as a Microsoft Access database on a Windows PC, or an Oracle 9i Lite database on a Palm or Pocket PC handheld device. For a complete list of the currently supported properties and methods, see these documents:
ODBC Table of ContentsJargon Software Methods
Jargon Reader supports a variety of properties and methods that have been developed by Jargon Software for host calls, graphical components and other specialized features. For a complete list of the currently supported properties and methods, see these documents:
Component Properties