Microsoft® JScript®
What Is JScript?
JScript is the Microsoft implementation
of the ECMA 262 language specification. It is a full implementation, plus
some enhancements that take advantage of capabilities of Microsoft Internet
Explorer.
It is implemented as a fast, portable, lightweight
interpreter for
use in World Wide Web browsers and other applications that use Microsoft®ActiveX®
Controls, Automation servers, and Java applets.
JScript is an interpreted, object-based
scripting language. Although it has fewer capabilities than full-fledged
object-oriented languages like C++ and Java, JScript is more than sufficiently
powerful for its intended purposes.
JScript is a loosely typed language. That means you do not have
to declare the data types of variables explicitly. Moreover, in many cases
JScript performs conversions automatically when they are needed. For instance,
if you try to add a number to an item that consists of text (a string),
the number is converted to text.
For full details of the language implementation,
consult the language reference.
LIMITATION
JScript is not a cut-down version of any other
language (it is only distantly and indirectly related to Java, for example),
and it is not a simplification of anything. It is, however, limited. You
cannot
write standalone applications in it, for example, and it has little
capability for reading or writing files.
Moreover, JScript scripts can run only in the
presence of an interpreter, either in a Web server or a Web browser.
Frequently Asked Questions
on JScript
How do I get JScript?
JScript is currently available as part of Microsoft®
Internet Explorer and Microsoft® Internet Information Server. You can
download the Internet Explorer.
Or you can download the engines directly from
the Scripting Engines download page.
Where can I find JScript documentation?
Documentation can be found at the JScript Documentation
page.
Where can I find JScript support?
Support options for JScript can be found at the
Active
Scripting support page.
Where can I find Internet Explorer Object Model
documentation?
The Object Model documentation for Microsoft®
Internet Explorer 5.0 provides an overview of the object model, sample
code , and reference information. It also describes the methods, properties,
and events used with the scripting engines in Internet Explorer. This can
be found in the MSDN Online Web Workshop.
What are some of the known issues of Internet
Explorer 5.0?
A complete list of known issues can be found at
the Microsoft®
Internet Explorer 5.0 support page
How does JScript compare to VBScript and Java?
When used in Microsoft® Internet Explorer, JScript
is directly
comparable to Microsoft® Visual Basic®
Scripting Edition . Like
VBScript, JScript is a pure interpreter that processes
source code
embedded directly in the HTML. JScript code, like
VBScript
code, does not produce stand-alone applets but is
used to
add intelligence and interactivity to HTML documents.
For developers familiar with Java, C or C++, JScript
provides familiar syntax and language features.
What platforms will support JScript?
JScript is available or under development for Windows®95 and
Windows®NT (including native versions for the Alpha architecture),
16-bit Windows, Sun Solaris and HP/UX and Macintosh.
What Objects, Methods, Properties, and Events
can I use?
There are four separate classes of objects available
within JScript:
Built-in Objects
provided by the JScript engine
User-defined objects
created by the script author.
Objects provided
by Internet Explorer
Objects provided
by the Web page author
The JScript engine provides the core run-time functionality,
including a minimal set of basic objects such as Math, Date, String, etc..
The vast majority of objects used in scripting are provided by Internet
Explorer, such as window, document, etc. In general, anything that is specific
to the Internet is provided by Internet Explorer, and anything that is
generally useful is provided directly in JScript. The Web author can insert
additional objects through the <OBJECT> HTML tag.
How can I write HTML text to the window?
You can use the document.write method to
write any text, HTML or otherwise, to the window. These commands must be
executed before the document has finished loading. The best way is to execute
JScript commands that are inline, not subroutines or functions that are
triggered by events. You might want to mention that it's a good idea to
put Jscript code that writes into the current document in the <HEAD>
section of an HTML document, not in the <BODY>.
How can I change the content of another frame?
Use parent.frames[1].location.href="filename.htm"
Note: Frames start
numbering at 0, so if you have 2 frames, they are
frames[0] and frames[1].
You can use the name of the frame instead of the
index reference, as in:
parent.rightframe.location.href="filename.htm"
How can I get the value of an object property
or variable in another frame?
Use top.framename.varname or parent.framename.varname.
To get the
value of a control property use top.framename.control.property.
How can I access an object in another frame?
top.framename.objectID (or parent...) example:
parent.controls.Axa1.FireImportedEvent 101
What is ECMAScript?
The ECMAScript standard describes a Web scripting
language that can
enrich and enliven Web pages in a Web browser. ECMAScript
is the
only standard scripting language on the Web; it
is based on the
ECMA-262 specification, which outlines an object-oriented
programming language for performing computations
and manipulating
objects within a host environment, such as the browser.
The complete
ECMA-262 specification can be found at
http://www.ecma.ch/stand/ecma-262.htm.
How is Microsoft supporting ECMAScript?
Microsoft JScript® 3.1, in Microsoft Internet
Explorer 4.0,
was the first scripting language to fully conform
to the
ECMA-262 specification. JScript Version 5.0
builds on
that standards commitment by introducing new features
that will be part of the upcoming Second Edition
of the
ECMA-262 standard . Microsoft is committed to the
standards process and to continuing its active
involvement in the ECMA process.
How do JScript and ECMAScript compare?
JScript 3.0 fully complies with the ECMAScript standard.
In addition to
this standard functionality, JScript 3.0 provides
features that Microsoft
submitted to be considered for the next ECMAScript
specification,
including the following: Conditional compilation.
Conditional
compilation provides the ability to take different
code paths depending
on specified run-time variables. This way, script
developers can write
script targeted to specific platforms and browsers
on the client machine.
Control flow enhancements. JScript provides new control
structures for
greater flexibility, including switch, label and
do…while. Regular
expressions. JScript supports the new RegExp object
for regular
expressions, which are patterns used to match character
combinations in
strings and provide the developer with a powerful
means of searching
strings for particular character combinations.
All these features
are being standardized in the next release of the
ECMAScript standard.
JScript 5.0 introduces structured exception handling
which was designed in conjunction with the ECMAScript
working group, including Netscape, Microsoft, IBM,
and
Nombas.
How do JScript and JavaScript compare?
JScript fully complies with ECMAScript. While JavaScript
1.1 and
JScript 2.0 served as the basis for ECMA standards
work, the standards process has resulted in significant
language improvements in the areas of Unicode support,
IEEE math functions and improved date functions.
Will JavaScript work in Internet Explorer 5.0?
Most functionality in JScript and JavaScript will
work across both the
Microsoft® Internet Explorer 5.0 and Netscape
Navigator 4.0 browsers.
The key issue here is that Internet Explorer 5.0
is already fully
ECMA-compliant. Internet Explorer 5.0 continues
to
lead in the standards arena, through its support
for key
standards such as ECMAScript.
Is Microsoft delivering JScript across platforms?
Yes. Microsoft is delivering JScript® 5.0 support
in all versions
of Microsoft® Internet Explorer 5.0, including
for
32-bit Windows® operating systems, 16-bit Windows,
Macintosh and UNIX platforms.
I'm writing an application that needs a scripting
language. Can I use JScript?
Yes. The binary and source code for Microsoft®
JScript® 5.0 is
available today. This means that applications supporting
the
Microsoft® ActiveX® scripting interface
can host JScript, and that
users of these applications can use JScript. In
addition, these
applications can host any other language that is
written to the ActiveX
scripting interface. Microsoft's scripting site
describes in more detail
how developers can distribute and use JScript in
their applications at no
charge.
To make script engine hosting even easier, Microsoft
has released the
Microsoft® Script Control. This control allows
any application that
supports COM to host script engines with only two
to three lines of
code. The control can be used in Microsoft®
Visual Basic® 5.0 and
provides documentation on how to use the control
within the Visual
Basic environment. The Microsoft Script Control
is free and can be
downloaded from the Microsoft Script Control pages
at the Microsoft
Scripting Technologies site.
Note that the sources can only be licensed if the
licensee is going to
port the code to a new OS.
How do I debug JScript?
By using the Microsoft Script Debugger or Microsoft
Visual
Interdev. More information on the debugger can be
found at the Microsoft Script Debugger pages at
the
Microsoft Scripting Technologies site. Visual
Interdev
information can be found at
http://msdn.microsoft.com/vinterdev
Does JScript have error handling like Java's exceptions
or VBScript's Error object?
Yes. In Version 5.0 JScript introduced structured
exception handling
using a mechanism very similar Java. For more
information goto the
JScript documentation site at
http://msdn.microsoft.com/scripting/JScript/doc/jsstmTryCatch.htm
.
In IE4, how do I determine the user's screen resolution
using JScript?
Consult the object model documentation on the following
properties:
screen.width
screen.height
screen.availWidth
screen.availHeight
document.body.clientWidth
document.body.clientHeight
How do I use the FileSystemObject in IE4?
The FileSystemObject does not ship with IE4 because
it is primarily
used by Active Server Pages scripts, not client
side scripts. You can get
the FileSystemObject by downloading the latest scripting
engines from
www.microsoft.com/scripting or by installing Windows
Script Host or
the NT Option Pack. The FileSystemObject can perform
unsafe
operations such as deleting files, so you will have
to set your security
settings appropriately to use the object from IE4.
How do I enumerate the members of a collection
in JScript? The for-in
loop doesn't work.
In Visual Basic, the for-in loop enumerates the
members of a collection:
For Each item In collection
document.write item
Next
Many Jscript users mistakenly assume that the JScript
for-in loop does
the same thing. It does not -- the JScript for-in
loop enumerates the
members of a JScript object:
var obj = new Object();
obj.foo = 1;
obj.bar = 2;
for (member in obj)
document.write(member + "=" + obj[member] + "<BR>"); // writes "foo
= 1 <BR> bar = 2 <BR>"
To enumerate the items in a collection, use the Enumerator
object:
for ( var myenum = new Enumerator(myCollection) ;
!myenum.atEnd() ; myenum.moveNext() )
document.write(myenum.item());
Enumerator objects are considerably more flexible
than for-each-in
loops. Several enumerators can be declared at the
same time for each
collection, enumerators can be passed around as
data,
they can be reset
to the beginning at any time, etc.
What are the Year 2000 Bug problems with JScript?
It is very easy to write code which will fail in
the year 2000 in JScript due
to a poor design decision in the Date object. Consider
the following
code:
var y1999 = new Date("1 January 1999");
var y2000 = new Date("1 January 2000");
alert(y1999.getYear());
// in all versions of IE and Netscape Navigator,
this returns "99".
alert(y2000.getYear());
// in JScript 1.0 (which shipped with IE3) this
returns "100". In Navigator and JScript 3 (IE4) this returns "2000".
In IE3, the getYear method returns the current year
minus 1900, for all
years past 1970.
In IE4, the getYear method returns two-digit years
for years 1900 to 1999
and four digit years for all others.
This is obviously a possible source of bugs. The
ECMA specification
mandates a new function, "getFullYear" which always
returns four-digit
dates. If you know that you will be using an ECMA-compatible
script
language like JScript 3, use this function. If not,
you can write your own
like this:
function GetFullYear(year){ return (year < 1000)
? year + 1900 : year; }
This gives the correct four-digit year when passed
the output of the
getYear() function, for all dates past the year
1000.
For more information read the scripting clinic on
script engines and the
Year 2000 at
http://msdn.microsoft.com/workshop/languages/clinic/Y2Kscript.asp
Microsoft Windows
Script Host
Overview
The Microsoft Windows Script Host (WSH) is a tool that
will allow you to run Visual Basic Scripting Edition and JScript
natively within the base Operating System, either on Windows 95 or
Windows NT 4.0. Using the scripting languages you already know you can
now write script to automate common tasks, and to create powerful
macros and logon scripts.