What's New in JScript 3.0

Nancy Winnick Cluts
Developer Technology Engineer
Microsoft Corporation

Updated: October 6, 1997
(See the " Changes for Internet Explorer 4.0 Final Release" section following.)

Changes for Internet Explorer 4.0 Final Release

Introduction

With the release of Microsoft® Internet Explorer version 4.0, you can now make the most of the functionality provided in Microsoft JScript® version 3.0. JScript is Microsoft's implementation of the ECMAScript scripting language (a standardized scripting language based on JavaScript). JScript is a full implementation of the ECMAScript scripting language with some enhancements for Internet Explorer. If you're like me, you might have the impression that JavaScript or JScript is just some cut-down version of the Java language. It is not. It is an object-oriented language in its own right. JScript is supported in earlier versions of Microsoft Internet Explorer and Internet Information Server (IIS), but the newest version of Internet Explorer (version 4.0) provides some new and improved functionality, including the addition of conditional compilation, enhancements to control flow, and additional operators. This article outlines the new things you can do with JScript 3.0 on Internet Explorer 4.0 and Microsoft Internet Information Server version 4.0. The Microsoft Scripting Web site has full documentation for JScript at http://www.microsoft.com/scripting/.

Conditional Compilation

If you are a script developer, you have to design your scripts to run on different browsers and on multiple platforms. It is a challenge to write script that is effective and useful and will also run on all clients and platforms. Prior to JScript 3.0, you would have had to use the "least common denominator" in your script. The addition of conditional compilation to the JScript 3.0 script engine provides the script developer the flexibility to write script targeted to specific platforms and browsers on the client machine. Lest anyone be confused by the term "conditional compilation," let me explain. JScript is an interpreted language that is compiled after the Web server or browser has interpreted the code. Conditional compilation provides the ability to take different code paths dependent on specified run-time variables. It also enables you comment out code for non-Internet Explorer 4.0 platforms.

Multiple clients

One of the best ways to use conditional compilation is to gracefully handle multiple disparate client browsers and platforms. Let's say that you want to utilize the new features in JScript 3.0 on a Web page, but you also need to maintain compatibility with older versions of Internet Explorer and Netscape Navigator. Before JScript 3.0, you would have had to write some script to determine which browser and what version and platform the user was running, plus write some server-side script to load a page compatible with that browser/version/platform. Using conditional compilation, you can put the JScript 3.0 features in a conditional compile section. This script runs only if the user is running a version of JScript 3.0; users of other browsers would not see the 3.0 code and the other script would run for them.

Debug vs. release

You can also use conditional compilation for debug versus release versions of your script. Many developers build a slightly different version of script in their development or debug environment than the final release script. Conditional compilation makes this easier. You can, for example, define a debug variable and, if that variable is set to TRUE, run some debug script (like alert variable values); otherwise, you can ignore the script.

The example below shows special comment delimiters that are used only if conditional compilation is activated by the @cc_on statement. Scripting engines that do not understand conditional compilation see only the alert statement identifying the need for a new scripting engine. It is strongly recommended that conditional compilation code be placed in comments:

//@cc_on
/*@if (@_jscript_version == 3)
   alert("JScript version 3")
   @else @*/
   alert("You need a more recent script engine.")
//@end

For more detailed information on conditional compilation, refer to the JScript Web site.

Enhancements to Control Flow

In addition to conditional compilation, JScript 3.0 provides enhancements to control the flow of execution of your script. Sometimes, you may need to control if and when certain script is run. JScript 3.0 uses control structures to facilitate this work. This section documents the enhancements to control flow.

Labels

Labels enable you to provide an identifier of script to Internet Explorer 4.0 that can be used in conjunction with the break and continue statements.

Break

The break statement terminates the current loop. If used in conjunction with a label, it terminates the associated statement. Executing the break statement exits from the current loop or statement, and begins script execution with the statement immediately following.

The syntax of the break statement is as follows:

break [label];

The label argument is optional and specifies the label of the statement from which you are breaking. For example, if you want to break on the label done, you can write:

break done ;

You typically use the break statement in switch statements and while, for, for...in, or do...while loops to stop a statement from executing. You most commonly use the label argument in switch statements, but it can be used in any statement, whether simple or compound.

Continue

The continue statement stops the current iteration of a loop and starts a new iteration. The label argument, listed in the syntax below, is optional and specifies the iteration to which the continue statement applies.

continue [label];

You can use the continue statement only inside a while, do...while, for, or for...in loop. Here's how the program flow is continued for each type of loop:

Switch

The switch statement enables the execution of one or more statements when a specified expression's value matches a label. This is a very familiar statement to C developers. The syntax for the switch statement is as follows:

switch (expression) {
   case label :
      statementlist

case label :
   statementlist
...
default :
   statementlist
}

The following example tests an object for its type using the switch statement:

function MyObject() {
...}
switch (object.constructor){
   case Date:
   ...
   case Number:
   ...
   case String:
   ...
   case MyObject:
   ...
   default:
   ...
}

Do...while

In JScript 3.0, this combination of statements tells the scripting engine to execute a statement block once, and repeat execution of the loop until a condition or expression evaluates to FALSE. You can use this control structure, for example, to fill in a list with values until the list is empty (that is, checking for the next item returns FALSE). The syntax for the do…while statement is as follows:

do
statement
while (expression) ;

Note that the value of expression is not checked until after the first iteration of the loop, guaranteeing that the loop is executed at least once. Thereafter, it is checked after each succeeding iteration of the loop. So, if you are writing some script that uses this structure, be sure that you want the statement to execute at least once.

Date and Time: getVarDate

The getVarDate method (methods are the name given to functions in JScript) enables interoperation between ActiveX® controls and JScript objects with regard to date. An ActiveX control could provide a date as a date variant (VT_DATE) and JScript 3.0 enables the user of this date to create a new JScript date based on the data type. For example:

<Script Language=JScript>

/* Create a date object from the variant date passed by an ActiveX control. */
var x = new Date(VariantDate);
</Script>

To provide the ability to convert the JScript date back into a VT_DATE, the Date object has a getVarDate method. Calling this method returns a VT_DATE.

<Script Language=JScript>
//Create a new date object.
var x = new Date();
//Set the date of my ActiveX control to the VT_DATE from the JScript object.
myActiveXObject.date = x.getVarDate;

</Script>

Operators

Operators are used for functionality including arithmetic (such as addition, subtraction, and multiplication), bitwise comparisons, and to assign values to variables. In JScript 3.0, new operators have been added to the scripting engine that enable the developer to check equality and identity.

Identity (===) and Identity (!==)

These two new operators provide JScript with a means of checking if two variables are identical (that is, the value and type are the same). The identity (=== and !==) operators behave exactly like to the equality operators (== and !=). The equality operators do some type coercions before comparing while === and !== do not coerce types.

Regular Expressions and Pattern Matching

Regular expressions are patterns used to match character combinations in strings and provide the developer with a powerful means of searching strings for particular character combinations. This is especially useful when searching large datasets for particular expressions, sorting information, or matching information entered by the user. Below is an example of pattern matching using the match method:

<SCRIPT LANGUAGE="JScript">
s = "For more information, see Chapter 3.4.5.1"
pattern = /(Chapter \d+(\.\d)*)/ //The parentheses are used for memory.
found = s.match(pattern) 
document.write(found)
</SCRIPT >

JScript 3.0 also supports the new RegExp object for regular expressions. This object uses properties and methods like any other JScript object. For full details about regular expressions, pattern matching, and the RegExp object, refer to the JScript 3.0 documentation.

Debugger Support

The Microsoft Script Debugger ships with Internet Information Server 4.0 and is freely available from the Microsoft Internet Information Server Web site (http://www.microsoft.com/iis/default.asp). It enables developers to interactively debug active script in any application. To assist with this endeavor, JScript 3.0 has added debug functionality to the scripting engine as well as a new keyword: debugger.

Putting the debugger keyword into any JScript code launches the script debugger and enables the developer to debug the JScript script.

Summary

In this article, I've covered only what is new in JScript 3.0 for Internet Explorer 4.0. For those of you who are new to JScript, more information, samples, and the latest version of JScript are available for download from the Microsoft JScript Web site at http://www.microsoft.com/jscript/.

Appendix: JScript? JavaScript? ECMAScript? What's the Deal Here?

You may be confused by the term ECMAScript that I've been using in this article. You're in good company. Here's the deal. ECMA (European Computer Manufacturers Association) is a European-based association for standardizing information and communications systems. The standard recently approved, known as ECMA-262, is based on joint submissions from Microsoft and Netscape. JScript 3.0 is Microsoft's implementation of the new ECMA-262 scripting language (you can read the press release on the Microsoft JScript Web site at http://www.microsoft.com/jscript/us/techinfo/js3pr.htm). JavaScript is a scripting language written by Netscape that preceded the ECMA standard. There's an excellent introduction to JavaScript available on the Web (http://www.cc.ruu.nl/~goyarts/javascript/javascr.htm), if you'd like to read up on it. Basically, when talking about JScript or JavaScript, we are talking about implementations of the same standard scripting language, ECMA—the implementations are just marketed by different companies.

Did you find this article useful? Gripes? Compliments? Suggestions for other articles? Drop me a line at nancycl@microsoft.com and let me know.