Cari di JavaScript 
    JavaScript Manual
Daftar Isi
(Sebelumnya) Chapter 13. Global Functions, ...getOptionValueCount, isNaN, Nu ... (Berikutnya)

debug

Displays a JavaScript expression in the trace facility.

Server-side function

Implemented in

LiveWire 1.0

Syntax

debug(expression)

Parameters

expression
Any valid JavaScript expression.

Description

The debug function is a top-level server-side JavaScript function that is not associated with any object.

Use this function to display the value of an expression for debugging purposes. The value is displayed in the trace facility of the Application Manager following the brief description "Debug message:".

Examples

The following example displays the value of the variable data:

debug("The final value of data is " + data)

deleteResponseHeader

Removes information from the header of the response sent to the client.

Server-side function

Implemented in

Netscape Server 3.0

Syntax

deleteResponseHeader(field)

Parameters

field
A field to remove from the response header.

Description

You can use the deleteResponseHeader function to remove information from the header of the response you send to the client. The most frequent use of this function is to remove the default content-type information before adding your own content-type information with addResponseHeader.

For more information, see addResponseHeader.


escape

Returns the hexadecimal encoding of an argument in the ISO-Latin-1 character set.

Core function

Implemented in

Navigator 2.0, LiveWire 1.0

Syntax

escape("string")

Parameters

string
A string in the ISO-Latin-1 character set.

Description

The escape function is a top-level JavaScript function that is not associated with any object. Use the escape and unescape functions to add property values manually to a URL.

The escape function encodes special characters in the specified string and returns the new string. It encodes spaces, punctuation, and any other character that is not an ASCII alphanumeric character, with the exception of these characters:

* @ - _ + . /

Examples

Example 1. The following example returns "%26":

escape("&")
Example 2. This statement

escape("The_rain. In Spain, Ma'am")
returns

"The_rain.%20In%20Spain%2C%20Ma%92am":
Example 3. In the following example, the value of the variable theValue is encoded as a hexadecimal string and passed on to the request object when a user clicks the link:

<A HREF=Q"mypage.html?val1="+escape(theValue)Q)>Click Here</A>

See also

unescape


eval

Evaluates a string of JavaScript code without reference to a particular object.

Core function

Implemented in

Navigator 2.0

Syntax

eval(string)

Parameters

string
A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.

Description

The argument of the eval function is a string. If the string represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.

If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script.

eval is also a method of all objects. This method is described for the Object class.

Examples

The following examples display output using document.write. In server-side JavaScript, you can display the same output by calling the write function instead of using document.write.

Example 1. Both of the write statements below display 42. The first evaluates the string "x + y + 1"; the second evaluates the string "42".

var x = 2
var y = 39
var z = "42"
document.write(eval("x + y + 1"), "<BR>")
document.write(eval(z), "<BR>")
Example 2. In the following example, the getFieldName(n) function returns the name of the specified form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element.

var field = getFieldName(3) 
document.write("The field named ", field, " has value of ",
   eval(field + ".value"))
Example 3. The following example uses eval to evaluate the string str. This string consists of JavaScript statements that open an Alert dialog box and assign z a value of 42 if x is five, and assigns 0 to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z.

var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; "
document.write("<P>z is ", eval(str))
Example 4. In the following example, the setValue function uses eval to assign the value of the variable newValue to the text field textObject:

function setValue (textObject, newValue) {
   eval ("document.forms[0]." + textObject + ".value") = newValue
}
Example 5. The following example creates breed as a property of the object myDog, and also as a variable. The first write statement uses eval('breed') without specifying an object; the string "breed" is evaluated without regard to any object, and the write method displays "Shepherd", which is the value of the breed variable. The second write statement uses myDog.eval('breed') which specifies the object myDog; the string "breed" is evaluated with regard to the myDog object, and the write method displays "Lab", which is the value of the breed property of the myDog object.

function Dog(name,breed,color) {
   this.name=name
   this.breed=breed
   this.color=color
}
myDog = new Dog("Gabby")
myDog.breed="Lab"
var breed='Shepherd'
document.write("<P>" + eval('breed'))
document.write("<BR>" + myDog.eval('breed'))

See also

Object.eval method


flush

Sends data from the internal buffer to the client.

Server-side function

Implemented in

LiveWire 1.0

Syntax

flush()

Parameters

None.

Description

To improve performance, JavaScript buffers the HTML page it constructs. The flush function immediately sends data from the internal buffer to the client. If you do not explicitly call the flush function, JavaScript sends data to the client after each 64KB of content in the constructed HTML page.

Use the flush function to control when data is sent to the client. For example, call the flush function before an operation that creates a delay, such as a database query. If a database query retrieves a large number of rows, you can flush the buffer after retrieving a small number of rows to prevent long delays in displaying data.

Because the flush function updates the client's cookie file as part of the HTTP header, you should perform any changes to the client object before flushing the buffer, if you are using client cookie to maintain the client object. For more information, see Writing Server-Side JavaScript Applications.Do not confuse the flush method of the File object with the top-level flush function. The flush function is a top-level server-side JavaScript function that is not associated with any object.

Examples

The following example iterates through a text file and outputs each line in the file, preceded by a line number and five spaces. The flush function then causes the client to display the output.

while (!In.eof()) {
   AscLine = In.readln();
   if (!In.eof())
      write(LPad(LineCount + ": ", 5), AscLine, "");
   LineCount++;
   flush();
}

See also

write


getOptionValue

Returns the text of a selected OPTION in a SELECT form element.

Server-side function

Implemented in

LiveWire 1.0

Syntax

getOptionValue(name, index)

Parameters

name
A name specified by the NAME attribute of the SELECT tag

index
Zero-based ordinal index of the selected option.

Returns

A string containing the text for the selected option, as specified by the associated OPTION tag.

Description

The getOptionValue function is a top-level server-side JavaScript function not associated with any object. It corresponds to the Option.text property available to client-side JavaScript.

The SELECT tag allows multiple values to be associated with a single form element, with the MULTIPLE attribute. If your application requires select lists that allow multiple selected options, you use the getOptionValue function to get the values of selected options in server-side JavaScript.

Examples

Suppose you have the following form element:

<SELECT NAME="what-to-wear" MULTIPLE SIZE=8>
   <OPTION SELECTED>Jeans
   <OPTION>Wool Sweater
   <OPTION SELECTED>Sweatshirt
   <OPTION SELECTED>Socks
   <OPTION>Leather Jacket
   <OPTION>Boots
   <OPTION>Running Shoes
   <OPTION>Cape
</SELECT>
You could process the input from this select list in server-side JavaScript as follows:

<SERVER>
var loopIndex = 0
var loopCount = getOptionValueCount("what-to-wear") // 3 by default
while ( loopIndex < loopCount ) {
   var optionValue = getOptionValue("what-to-wear",loopIndex)
   write("<br>Item #" + loopIndex + ": " + optionValue + "")
   loopIndex++
}
</SERVER>
If the user kept the default selections, this script would return

Item #1: Jeans
Item #3: Sweatshirt
Item #4: Socks

See also

getOptionValueCount

(Sebelumnya) Chapter 13. Global Functions, ...getOptionValueCount, isNaN, Nu ... (Berikutnya)