Chapter 6. Scripting with JavaScript

Table of Contents

Overview
Number
Boolean
String
Object
Array
References
Scriptlets
RenderIf
OnRenderBegin
OnRenderEnd
OnLayout
Script Editor
JavaScript Security
Introduction
Steps to protect users from malicious Javascripts
Configure Security Permissions
Verify that Security Policies are Taking Effect
JavaScript Cookbook
Alternating colours
Hiding and showing components
Using parameters to dynamically set values
Accessing Java classes

Overview

Elixir Report Designer supports JavaScript 1.6 as standardized by the European Computer Manufacturer's Association(ECMA) as ECMA Script version 3. This document details some of the features of JavaScript and their use within the Report framework.

In Elixir Report Designer, scripts are always rendered in the order that the bands appear in the output. Within a band, scripts in RenderIf will be rendered first. It will be followed by OnRenderBegin, OnRenderEnd, then OnLayout. As for the element(s) in each band, scripts of each element will run in the order the elements appear in the Shapes tree (Z-order).

JavaScript is a weakly typed, object-based scripting language modeled on Java syntax. It is a case-sensitive language and the following key-words are reserved. It is a case sensitive language and the following keywords are reserved.

Table 6.1. JavaScript Keywords

breakelseinstanceoftrue
casefalsenewtry
catchfinallynulltypeof
continueforreturnvar
defaultfunctionswitchvoid
deleteifthiswhile
dointhrowwith

Three primitive types are available: Numbers, Booleans and Strings, along with two compound types: Objects and Arrays.

Number

Numbers in JavaScript are represented internally using 64-bit floating point values - there is no integer type. Numbers may be written in decimal form: 10, 20.5, 30, exponent form: 3e-2 or hexadecimal: 0xFF.

JavaScript supports all conventional mathematical operators, including +,-,*,/ and % (modulus). The Math object provides additional functionality, such as sqrt, power, sin, cos, tan etc. Certain operations may yield errors with invalid input, for example Math.sqrt(-1) produces the result NaN - a special value which indicates the result is Not A Number.

Boolean

The Boolean type has two values: true and false. Boolean can be used as a function: Boolean(x) where the result is true unless x is 0, NaN, null, undefined or "" (the empty string).

String

The String type represents a sequence of characters and is delimited with either single or double quotes: "Hello World", 'Elixir Report'. Strings can be concatenated with the + symbol:

"Hello " + "World"

Other types are automatically converted to Strings when concatenated with them:

count = 5; message = "Hello" + count

This example also shows that multiple statements are separated by semi-colons. Strings support many additional functions, including: length, charAt, indexOf, match, replace, split, substring etc. Strings in JavaScript are immutable - just like in Java - once created they cannot be changed.

Object

Objects are compound types because they can contain multiple properties (named values of any type). Elixir Report Designer provides objects which represent all of the components that can be manipulated through the Report Designer. Most use of scripts in Elixir Report Designer consists of manipulating the properties of these objects during the report rendering process.

Where JavaScript is used to access Java objects, it is possible to use two alternate forms of access. You can access properties through traditional get and set methods, or access the properties directly, which JavaScript then translates into the appropriate get and set operations. For example, these two lines have identical behaviour:

component.setBackgroundColor("Red");
component.backgroundColor = "Red";

A similar situation applies for get:

c = component.getBackgroundColor();
c = component.backgroundColor;

You can choose the style of property access that suits you.

Objects loaded from a datasource may sometimes have field names that are not valid names in JavaScript. Such fields can still be accessed by using a named lookup of the value. For example, if the field name is 2000 (which obviously isn't a valid variable name) you can access the value with

this["2000"]

Array

Arrays are compound types which contain indexed values of any type. An array is created using the new keyword:

a = new Array(10)

This allocates ten slots in the array named a, that are indexed a[0] - a[9]. Arrays can also be created with initial values using an alternate syntax:

a = ["Hello", "World", 123 ]

This creates an array named a with three slots, where a[0] is the string "Hello", a[1] is the string "World" and a[2] is the number 123.

References

For more detailed information on JavaScript, please refer to:

  • JavaScript Pocket Reference, David Flanagan, (O'Reilly)

  • JavaScript: The Definitive Guide, David Flanagan, (O'Reilly)

  • ECMAScript Language Specification (ECMA-262) (www.ecma.ch)