Click to return to the Languages     
Web Workshop  |  Languages & Development Tools

What's New in JScript 3


Nancy Winnick Cluts
Developer Technology Engineer
Microsoft Corporation

Updated October 6, 1997

Contents
Changes for Internet Explorer 4.0 Final Release
Introduction
Conditional Compilation
  Multiple Clients
  Debug Versus Release
Enhancements to Control Flow
  JScript 3.0, labels in
  JScript 3.0, break statements in
  JScript 3.0, continue statements in
  JScript 3.0, switch statements in
  JScript 3.0, do...while statements in
  JScript 3.0, getVarDate method in
Operators
  Identity (===) and Identity (!==)
Regular Expressions and Pattern Matching
Debugger Support
Summary
Appendix: JScript? JavaScript? ECMAScript? What's the Deal Here

Changes for Internet Explorer 4.0 Final Release

TopBack to top

Introduction

With the release of Microsoft® Internet Explorer 4.0, you can now make the most of functionality provided in JScript™ 3.0. JScript is Microsoft's implementation of the ECMAScript scripting language (formerly known as JavaScript™). JScript is a full implementation of the ECMAScript scripting language with some enhancements for Internet Explorer. If you are 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 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 compiliation, 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 IIS 4.0. The Microsoft Scripting Web site Non-MSDN Online link has full documentation of JScript.

TopBack to top

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 Non-MSDN Online link 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 code has been interpreted by the Web server or browser. 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.

TopBack to top

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.

TopBack to top

Debug Versus 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 Non-MSDN Online link.

TopBack to top

Enhancements to Control Flow

In addition to conditional compiliation, 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.

TopBack to top

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.

TopBack to top

Break

The break Non-MSDN Online link 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.

TopBack to top

Continue

The continue Non-MSDN Online link 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:

TopBack to top

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: 
 ...
}

TopBack to top

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 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.

TopBack to top

Date and Time: getVarDate

The getVarDate method (methods are the name given to functions in JScript) enables interoperation between ActiveX™ objects 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 Object */ 
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 object to the VT_DATE from the 
//JScript object
myActiveXObject.date = x.getVarDate;

</Script>

TopBack to top

Operators

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

TopBack to top

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.

TopBack to top

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.

TopBack to top

Debugger Support

The Microsoft Script Debugger ships with Internet Information Server 4.0 and is freely available from the IIS 4.0 Web site Non-MSDN Online link. 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 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.

TopBack to top

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, here are links to more information and samples, and to download the latest version of JScript for your own use:

TopBack to top

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 Non-MSDN Online link about it if you wish). JavaScript is a scripting language written by Netscape that preceded the ECMA standard. There's an excellent introduction to JavaScript Non-MS link available on the Web, 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.



Back to topBack to top

Did you find this material useful? Gripes? Compliments? Suggestions for other articles? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.