Export (0) Print
Expand All

Troubleshooting Your Scripts 

All programming languages include potential pitfalls and surprises for novice and experienced users. Here are some potential trouble areas that you may encounter as you write JScript scripts.

Syntax Errors

Because syntax is much more rigid in programming languages than in natural languages, it is important to pay strict attention to detail when you write scripts. If, for example, you intend that a particular parameter be a string, you will encounter trouble if you forget to enclose it in quotation marks.

Order of Script Interpretation

In a Web page, JScript interpretation depends on each browser's HTML parsing process. A script inside the <HEAD> tag is interpreted before text within the <BODY> tag. Consequently, objects that are created in the <BODY> tag do not exist when the browser parses the <HEAD> element and cannot be manipulated by the script.

NoteNote

This behavior is specific to Internet Explorer. ASP and WSH have different execution models (as would other hosts).

Automatic Type Coercion

JScript is a loosely typed language with automatic coercion. Consequently, despite the fact that values having different types are not strictly equal, the expressions in the following example evaluate to true.

"100" == 100;
false == 0;

To check that both the type and value are the same, use the strict equality operator, ===. The following both evaluate to false:

"100" === 100;
false === 0;

Operator Precedence

The order of operation execution during the evaluation of an expression depends more on operator precedence than on the order of operators in the expression. Thus, in the following example, multiplication is performed before subtraction even though the subtraction operator appears before the multiplication operator in the expression.

theRadius = aPerimeterPoint - theCenterpoint * theCorrectionFactor;

For more information, see Operator Precedence.

Using for...in Loops with Objects

When a script steps through the properties of an object with a for…in loop, the order in which the fields of the object are assigned to the loop counter variable are not necessarily predictable or controllable. Moreover, the order may be different in different implementations of the language. For more information, see for...in Statement.

with Keyword

Although the with keyword is convenient for addressing properties that already exist in a specified object, it cannot be used to add properties to an object. To create new properties in an object, you must refer to the object specifically. For more information, see with Statement.

this Keyword

Although the this keyword exists inside the definition of an object, you cannot ordinarily use this or similar keywords to refer to the currently executing function if the function is not an object definition. If the function is to be assigned to an object as a method, a script can use the this keyword within the function to refer to the object. For more information, see this Statement.

Writing a Script That Writes a Script in Internet Explorer or ASP.NET

The </SCRIPT> tag terminates the current script if the interpreter encounters it. To display "</SCRIPT>" itself, write this as two or more strings, for example, "</SCR" and "IPT>", which the script can then concatenate in the statement that writes them.

Implicit Window References in Internet Explorer

Because more than one window can be simultaneously open, any window reference that is implicit points to the current window. For other windows, you must use an explicit reference.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft