Methods and Functions


One of the strengths of JScript is that a lot of the difficult work has been done for you, with methods and functions covering a wide range of functionality. From performing calculations to manipulating dates and strings, JScript has a method or function to suit you.

Conversion Functions

Since JScript variables can be of many different types, there are a couple of conversion functions you will be using often if you want to ensure that a particular field value is either an integer or floating decimal point number. These conversion functions are listed in Table B-4.

Table B-4: Conversion Functions

Operator

Description

Example Usage

parseInt

For parsing other variable types to integer

parseInt(Quantity);

parseFloat

For parsing other variable types to a floating decimal point number

parseFloat(OrderTotal);

String Functions

String functions within JScript treat strings like an array and can be used to find substrings within a string, change the case of a string, concatenate two strings, and more. Table B-5 lists and describes the string functions.

Table B-5: String Functions

Function

Description

Usage

charAt

Used to return a character at a certain position within a string.

String.charAt(position);
MyString.charAt(2);
“David”.charAt(3);
Returns “v”

indexOf

Used to return the position at which a substring exists within a string, searching from left to right. Can also specify a second argument as a starting point.

String.indexOf(substring);
MyString.indexOf(“Sales”);
MyString.indexOf(“al”,2);
”David”.indexOf(“vi”);
Returns 3

lastIndexOf

Used to return the position at which a substring exists within a string, searching from right to left. Can also specify a second argument as a starting point.

String.indexOf(substring);

MyString.indexOf(“Market”);
MyString.indexOf(“ke”,2);
”Mack”.indexOf(“ack”);

Returns 2

split

Can be used to split a string into substrings using a delimiter (spaces, and so on) and put the resulting substrings into an array.

String.split(“ “);
MyString.split(“,”);
“My old dog”.split(“ “);

toLowerCase

Used to convert a string to lowercase.

String.toLowerCase();
MyString.toLowerCase();
“David”.toLowerCase();

toUpperCase

User to convert a string to uppercase.

String.toUpperCase();
MyString.toUpperCase();
“Mack”.toUpperCase();

length

Used to determine the length of a string.

String.length;
MyString.Length;
“David Mack”.Length;

Date and Time Functions

The date and time functions within JScript are easy to use and provide methods to pull apart a date and work with the components. All the examples in Table B-6, which lists the date and time functions, assume that the variable InvoiceDate is a date-time field with a value of 12/11/2003 8:10:57 AM.

Table B-6: Date and Time Functions

Function

Description

Example Usage

getDate

Returns the day-of-month value (1 to 31) from a date field

InvoiceDate.getDate()

Returns 11

getDay

Returns the day of the week, where:
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday

InvoiceDate.getDay()

Returns 4

getFullYear

Returns the full year from a date field

InvoiceDate.getFullYear()

Returns 2003

getHours

Returns the hours value from a date field

InvoiceDate.getHours()

Returns 8

getMinutes

Returns the minutes value from a date field

InvoiceDate.getMinutes()

Returns 10

getMonth

Returns the month value from a date field, where:
0 = January
1= February
2 = March
3 = April
4 = May
5 = June
6 = July
7 = August
8 = September
9 = October
10 = November
11 = December

InvoiceDate.getMonth()

Returns 11

getSeconds

Returns the seconds value from a date field

InvoiceDate.getSeconds()

Returns 57

getTime

Returns an integer value representing the number of milliseconds between midnight, January 1, 1970 and the time value in the Date object

InvoiceDate.getTime()

Returns 1071090659513

getYear

Returns the year value from a date field

InvoiceDate.getYear()

Returns 2003

Array Functions

Arrays can easily be created in JScript by a simple declaration. And then any array elements can be referenced using square brackets []. Arrays in JScript are zero-based, so the first element will always be 0, as shown in the following example:

var myArray = new Array();
for (i = 0; i < 10; i++)
{
myArray[i] = i;
}




How to Do Everything with Microsoft Office InfoPath 2003
How to Do Everything with Microsoft Office InfoPath 2003 (How to Do Everything)
ISBN: 0072231270
EAN: 2147483647
Year: 2006
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net