Chapter 2 The DynaScript Language
You can define and use functions in scripts. This allows you to encapsulate a set of instructions once in a document as a function, then use it several times throughout the document (by calling the function). For example:
<!--SCRIPT function addVar( x, y, z ) { return x + y + z; } document.writeln( addVar( 1, 2, 3 ) ); -->
You must define a function before it
can be used, since the application server processes an HTML document
from top to bottom. Also, you must use parentheses when calling
a function, even if it doesn't take away arguments.
A function can accept string, number, or object parameters.
A function can perform a set of statements without returning a value. For example:
PrintInvoices();
A function can also return a value to the caller
by including a return
statement.
In this case, the function is typically called as part of an expression:
nextCustomerName = GetCustomerName(customerNum);
You can define functions inside a
particular document. For more flexibility, you can define them in
a separate script, which can then be referenced by any documents
that use the function (using the import
statement).
For more information on the function
, return
,
and import
statements,
see "Statements".
The arguments object can be used with a function to return an array that contains the arguments that were passed to the currently executing function. For example:
<!--SCRIPT function addVar( x, y, z ) { document.writeln( addVar.arguments[0] ); document.writeln( arguments ); return x + y + z; } document.writeln( addVar( 1, 2, 3 ) ); -->
returns:
1 { 1, 2, 3 } 6
The arguments object has a length
property
that contains the number of variables passed to the function.
The arguments object also has a callee
property
that can be used for recursion. The following example creates a
function dynamically and then recursively calls the same function
from within the function body:
<!--SCRIPT f = new Function ( "n", " if(n == 1) return 1; return n * arguments.callee(n-1);" ); document.WriteLn( f(4) ); -->
Dynamo supports the following ECMAScript built-in functions:
Dynamo provides the following built-in functions:
HKEY_CLASSES_ROOT
.
If a program ID is not listed in your Registry, the application
is not available for use.
Only available from Windows or NT CreateObject
is
available only on Windows 95 or NT platforms, and can be used only
with nonvisual ActiveX objects.
<HTML> <H1>Converting Temperatures </H1> <BODY> <P>This page allows you to convert a temperature from <I>Celsius</I> to <I>Fahrenheit</I> or from <I>Fahrenheit</I> to <I>Celsius</I>. <BR> <HR WIDTH="100%"> <P>Select the appropriate button and enter the temperature you would like to convert. <FORM METHOD=POST ACTION="convert.stm"> <OL> <LI><INPUT TYPE="radio" NAME="theType" value="CtoF" CHECKED>Celsius to Fahrenheit<BR> <INPUT TYPE="radio" NAME="theType" value="FtoC">Fahrenheit to Celsius<BR> <LI><INPUT TYPE="text" NAME="temperature" SIZE="4"> Temperature </OL> <P><INPUT TYPE="submit"></p> <P><INPUT TYPE="RESET" VALUE="Clear Form"></P> </FORM> </BODY> </HTML>
SybaseDemoObject.TempConvert
".
This template uses the methods of the SybaseDemoObject.TempConvert
to
calculate the request from the first template.
<HTML> <TITLE>convert.stm</TITLE> <BODY> <H1>Temperature</H1> <P>Your answer is: </P> <!--SCRIPT var conversionType = document.value.theType; var tempnumber = document.value.temperature; var DemoObj = CreateObject("SybaseDemoObject.TempConvert"); if ( conversionType == "CtoF" ){ // ConvertCtoF is a method of the // SybaseDemoObject.TempConvert object var c = DemoObj.ConvertCtoF(tempnumber); document.WriteLn( "The Celsius temperature of " + tempnumber + " is " + c + " on the Fahrenheit scale." ); } else { // ConvertFtoC is a method of the // SybaseDemoObject.TempConvert object var f = DemoObj.ConvertFtoC(tempnumber); document.WriteLn( "The Fehrenheit temperature of " + tempnumber + " is " + f + " on the Celsius scale." ); } --> </BODY> </HTML>
if( exists( session.logon ))...
Document.writeln( formatString("%1 students have %2 the exam", 32, "passed"));
32 students have passed the exam
sqlEscape
takes
two parameters. The first parameter is the string to be processed,
the second optional parameter is a Boolean that indicates whether
the backslash characters are to be escaped.
sqlEscape
causes
the special characters to be doubled up for the database to read
them correctly when being passed. If the string \site\system\doc's
was
being passed into a SQL statement against an Adaptive Server Anywhere
database it could be prepared as follows:
dir = sqlEscape( dir, true );
'\\site\\system\\doc''s'
.
Similarly, to prepare the dir
string
to be used against Adaptive Server Enterprise you would do something
like:
dir = sqlEscape( dir, false );
'\site\system\doc''s'
<!--SCRIPT xmlStr = '<BOOK><TITLE>Guiness World Record Book</TITLE>& <CHAPTER pages="10">This is chapter 1</CHAPTER>& <CHAPTER pages="30">This is chapter 2</CHAPTER>& <CHAPTER pages="20">This is chapter 3</CHAPTER>& </BOOK>' domDoc = toDOMDocument( xmlStr ); if( domDoc == null ) { document.writeln( site.GetErrorInfo() ); return; } document.writeln( domDoc.prettyPrint() ); -->
object
Is
the DynaScript object for which XML representation is required.
use_CDATA
Indicates
that you want to use CDATA to escape special XML characters. If use_CDATA
is
false, characters will be encoded with the normal ampersand method.
The default value is false.
index_tag
Specifies
a set of tags to use when unnamed members of an object are encountered.
This example uses toXMLString
to
display automobile service information:
<!--SCRIPT document.WriteLn("Car for Service") carObj = null; carObj.manufact = "Mazda"; carObj.model = "MX6"; carObj.year = "1998"; carObj.color = "blue"; carObj.owner.name = "Rick Smith"; carObj.owner.address = "21 King St. Waterloo"; carObj.work = "new brakes"; information = toXMLString( carObj ); document.WriteLn( information ); -->
The output from the above example looks something like this:
Car for service <manufac>Mazda</manufac> <model>MX6</model> <year>1998</year> <color>blue</color> <owner><name>Rick Smith<name> <address>21 King St. Waterloo<address></owner> <work>new brakes</new brakes>
The following example of the toXMLString
built-in
function sets use_CDATA
to
true and specifies which tag to use if a member has not been named:
<!--SCRIPT document.WriteLn("Parts for order"); part1.shop = "Jim & Son's Autobody"; part1.partname = "strut"; part1.sku = "123"; part2.shop = "Jim & Son's Autobody"; part2.partname = "clutch"; part2.sku = "456"; parts[0] = part1; parts[1] = part2; x = toXMLString(parts, true, "part"); document.WriteLn( x); -->
The output from the above example looks something like:
Parts for Order <part> <shop><![CDATA[Jim & Son's Autobody]]></shop> <partname>strut</partname> <sku>123</sku> </part> <part><shop><![CDATA[Jim & Son's Autobody]]></shop> <partname>clutch</partname> <sku>456</sku> </part>
typeof(
)
function is used to determine what values
have been received:
<!--SCRIPT SelectMultipleAction.ssc /* e.g. URL http://localhost/site/SelectMultiple Action.ssc?color=Red&color=Green&color=Blue */ if( exists( document.value.color ) ) { if( typeof( document.value.color) == 'object' ) { // multiple color values i = 0; while( exists( document.value.color[i] ) ) { document.writeln( document.value.color[i] ); i++; } } else { // single coloFur value document.writeln( document.value.color ); } } else { // no color selected document.writeln( 'no color was selected' ); } -->
document.writeln( xmlEscape( "<MyTag> Hello! </MyTag>" ) );
<MyTag> Hello! </MyTag>
document.writeln( xmlEscape( "Tom & Jerry", true ) );
"<![CDATA[Tom & Jerry]]>"
Copyright © 2001 Sybase, Inc. All rights reserved. |
![]() |