Remember the thrill of creating your first Web page and clicking your first hyperlink to another site? The excitement of surfing from California to Maine, from Australia to Finland? This interactive nature of the Web attracts millions of people to the Web every day.
With JScript, new dynamic elements let you go beyond the simple click-and-wait. Users will not just read your pages but also interact with them. Your pages come alive for any user, even with the slowest Internet connection. Users will get quick responses, because the interaction does not need to involve the server but can take place in the user's browser.
This interaction can change your pages into an application. Put together a few buttons, a text box, and some code to produce a calculator. Or an editor. Or a game. Or the "killer" JScript application that everyone wants. Users will save your JScript-enhanced pages to use your application again and again. We saved a Web page with a JScript calculator so we could have a calculator handy. We also have an HTML editor written in JScript saved on our hard drives.
JScript is a programming language that allows scripting of events, objects, and actions to create Internet applications. This chapter will introduce you to JScript and teach you the following:
In building Web pages, you present information to your audience. The design and layout should entice them to explore your site. Your hyperlinks provide several predefined but different paths to see your information.
With JScript, your pages come alive! Your pages respond to the requests of your audience beyond a simple click here or there. Many more interactive elements ar now available for exciting design and layout. Your users are no longer just readers. People will interact with your documents, not just read them. Your users can now interact with forms, change the look and feel of your Web documents, and use multiple windows.
With JScript, forms are a consideration in nearly every page you design. Text fields and text areas can dynamically change in response to user responses. Your audience will look for buttons, selections, option buttons, and check boxes. Your pages will change at their touch. The following examples show you how JScript makes forms come alive:
The source code in Listing 1.1 is easy to modify for you to tell your own jokes on the Web without giving away the punchline.
Listing 1.1 JSPUNCH.HTM-Code for JScript Punchline
All of the elements inside the window of a browser are available in JScript. You can dynamically change some of these elements, or you can examine the elements of one document and use that information to create a different document. Here are some examples of changing the look and feel of documents.
There are too many colors for users to choose from. Don't make them experiment; let them select some good combinations you have already tested.
Listing 1.2 shows the frames for the top level frame for HTML Analysis. It uses three files, HANALYSI.HTM, GUIDE.HTM, and HANALYS1.HTM, all of which are on the CD-ROM.
Listing 1.2 HANALYSI.HTM-Frames for HTML Analysis
<HTML><HEAD><TITLE>HTML Analysis by Ray Daly</TITLE></HEAD> <FRAMESET ROWS="80,300,*"> <FRAME SRC="hanalys1.htm" NAME="control"> <FRAME SRC="" NAME="displayhere"> <FRAME SRC="" NAME="analysis"> <FRAME SRC="guide.htm" NAME="guide"> </FRAMESET></HTML>
Listing 1.3 shows the code for the HTML Analysis itself.
Listing 1.3 HANALYS1.HTM-Code for HTML Analysis
The HTML Analysis application is not stable on all platforms. Make sure the URL is completely loaded before you do the analysis.
You can reformat pages for dramatic results. Instead of showing the entire document in a large frame, bring the source document into an extremely small frame. Then, display your reformatted document in the much larger frame. If the frame with your source is small enough, your users won't even know what the original looked like.
Internet Explorer 3.0 supports a new functionality-enhancing feature that provides inline support for a huge range of Live Objects, known as ActiveX controls. With Live Objects, developers can deliver rich multimedia content through Internet sites, allowing users seamlessly to view that content with ActiveX controls, which will perform a wide variety of tasks without launching any external helper applications.
There is no definitive definition of a scripting language. Sometimes the term is used to make a distinction from compiled languages such as C or C++. The term scripting is also used because a language will react to, control, or "script" a series of events. Even macro languages built into applications such as spreadsheets, databases, word processors, and multimedia applications are now often called scripting languages.
The purpose of most scripting languages is to extend the capability of an application. Just as the author of this book cannot imagine every creative use you will make of JScript, software authors cannot imagine every possible use of their applications. To make their products more versatile, they add a scripting language. With JScript, you have a scripting language to use your imagination on the Web.
Current uses of scripting languages may give you an insight into the potential of JScript. You probably know that macros are built into many applications. Apple's HyperCard contains a very powerful scripting feature. Perl is a scripting language used in many CGI scripts on the Web.
Traditionally, a macro feature was added to application software to allow a simple series of commands to be executed with a single keystroke. With great fanfare, publishers introduced this feature as a way to reduce repetitive tasks and save time. For example, a word processor's simple macro might change the entire style of a document.
Over time, the macro feature of various applications became complex scripting languages. As scripts became longer and nontrivial, they extended the software beyond its normal purpose. New and creative combinations of commands made the software the basis for entirely new applications-for example, a set of word processing scripts for maintaining a small mailing list.
These scripting languages in software are so sophisticated that they are the subject of college courses. Many universities now require courses in spreadsheet scripting for accounting and business students, art majors are learning scripting procedures for high-end graphics and multimedia packages, legal courses include using scripts to create documents, and computer science majors have a variety of courses involving scripting languages.
A defining factor of these types of scripting languages is that they work only with applications. Scripts in word processors add word processing features. Scripts in spreadsheets add spreadsheet features. These scripts do not go beyond the nature of the software, but they use the existing commands of that software. In our example, the mailing list script still works with words, the standard element of the word processor. This becomes a limitation on the usefulness of this script.
With the popularity of program suites such as Microsoft Office, Lotus SmartSuite, and Perfect Office, software publishers have started making the same scripting language work with more than one application. Not only is the same language used in each application, but the scripting language helps the applications work together. Microsoft expanded the role of Visual Basic to work with Microsoft Access and Excel. Lotus has developed LiveBasic for its product suite. The same logic, when applied to the World Wide Web, led to the development of JScript.
Historically, scripting has made several "killer applications." These are applications that define a whole new category of software, significantly expand the market, and provide a primary reason for people to use a computer. The first successful spreadsheet was VisiCalc, which mostly disappeared with the success of Lotus 1-2-3-the latter had scripting. There were many different database applications on the market before Ashton-Tate's dBase, but this product was programmable with a scripting language.
Scripting gave these applications a great feature that people wanted. Perhaps more important were the actual scripts people created to extend the capabilities of these products. The existing scripts became an investment in these products that prevented users from switching to competitive products, even if they had more features.
Scripting gave these applications a competitive edge. First, it was a feature that could be used to sell the product. Second, people actually started to use the feature and create significant new capabilities for these products. Third, these scripts create a whole new market with magazine articles, books, third-party software publishers, and training. Fourth, the continuing use of these scripts became an investment in these products by the user: Existing scripts often prevented users from switching to competitive products. And finally, even when a competitive product was introduced with new features, someone would introduce scripts that attempted to add these features into the existing products. Scripts allowed both publishers and users to advance.
The most notable use of scripting on the Macintosh is Apple's HyperCard program. This application lets you build a group of cards and hyperlink them together. The cards could contain not only text but multimedia files. The stack of cards that you construct could respond to user input.
The scripting language is such a strong element of HyperCard that many people consider HyperCard itself to be a language. Many Mac owners were initially disappointed with HTML because it lacked many of the abilities of HyperCard. In many ways, JScript brings some of the HyperCard features to the Web.
If you have used the Web, you have used Perl. It is the language used for the majority of CGI scripts. These are routines that run on Internet servers and respond to requests from browsers when a user completes a form. There are guestbooks, message boards, voting pages, surveys, and more that use Perl scripts.
Perl is an interpreted language. While you should be able to find a version of Perl for almost any computer platform, it was created for UNIX systems. It is now platform-independent. The vast majority of Perl scripts will run without modification on any system. Take a script written on UNIX, and it will run perfectly well on DOS.
CGI scripts are a type of script that responds to events. In this case, the event is a user submitting data from an HTML form. The attributes of a <FORM> include ACTION, which defines the script to process the data when it is submitted. For example:
will process the data from the form in a script called guestbook.pl. More than likely, this routine would store the data in a file and return an HTML page to the browser as feedback. It would probably say something like, "Thanks for your entry into our guestbook."
Perl is freely distributed on the Internet, but please see its license for more detail. You should be able to find a version for your system using any of the Internet search services. Larry Wall created and maintains it.
Perl's strength as a language is in manipulating files and text to produce reports. This capability, along with its associative arrays, makes it a natural fit for creating CGI scripts. In a few lines, you can process data and return an HTML document in response to an HTML form.
If you are a Perl programmer, you can rather quickly learn JScript. Both have a similar control structure and both are interpreted languages. Unlike Perl, JScript is object-based, but it is not nearly as complex. You might miss the text-processing capabilities of Perl, but you will find JScript a delightful new language to learn.
There are some cases where JScript is not the appropriate solution, but using Perl for a CGI script would fit the requirement. Generally, if you need to store information, you are going to have to do that on the server, and Perl would be a good choice.
Like other scripting languages that extend the capabilities of the application with which they work, JScript extends the standard Web page beyond its normal use. You have already seen in this chapter numerous ways to make your Web site come alive. Given the flexibility of the language, the only limit is your imagination. We must now consider how JScript works within HTML pages.
With the standard Web site, you get more information by clicking a hypertext link, and the server sends you another file. On a more interactive page, you complete a form, submit the results to the server, and wait for a response. In either case, you must wait for the server to send a new file. This information is almost always a new page, though it might be a multimedia file such as an audio clip or an animation.
With JScript-enhanced pages, there is JScript code embedded in the HTML code. The JScript can instantly provide you with information without waiting for the server or your Internet connection (see Figure 1.12). This information can come from user input, code "hidden" within the document, or other documents in frames or other windows.
Figure 1.12: With standard HTML pages, a Web site serves each page to the browser. With JScript-enhanced pages, the source for a page can be the existing page.
A JScript-enhanced page makes this new information visible by updating the contents of a form or by generating an entirely new document. In a JScript calculator (as seen earlier, in Figure 1.1), the form is updated when numbers are entered. In "Punchline" (refer to Figure 1.5), the user clicks the button, and a new document is created from the "hidden" punchline of the joke.
Like most any other computer language, JScript allows you to place comments within your code. Single-line and multiple-line comments are possible. A multiple line starts with the following two characters: /*
It ends with the two characters: */
Consider the following example:
/* This is the start of multiple lines of comments. This is the end */
To make a comment at the end of a line or on a single line, just use the characters // and everything after that until the end of the line will be considered a comment.
Between <SCRIPT> tags, you can write two types of code: direct statements and functions. Direct statements are executed by the browser as they are loaded. For example, objects are initialized in direct statements. Functions are blocks of code that are executed only by other code or events.
For example, mouse-click events usually trigger functions. Most of your programs will use both direct statements and functions.
Many existing HTML tags now have additional attributes to support JScript. For example, all elements of a form can now be identified with the NAME element. You should be familiar with the NAME attribute because it has long been used in creating anchors. Using NAME to identify objects in your documents will generally simplify your coding and debugging.
Another addition to HTML is the ability to recognize events such as mouse clicks, changes in text boxes, and the loading or unloading of pages. This is how the document recognizes the user interaction. These events are used to trigger JScript actions. The code can be quite straightforward, as in the following:
<FORM> <P>Click inside the box and then out to see change. <INPUT TYPE="text" NAME="sample" onChange = "sample.value = a" > <!-- ...after any change in this text box, put the value of a in the box --> </FORM>
The JScript code that is triggered by an event can be simple or complex. With simple actions, the entire code can be placed in the event element. This is shown in the previous example with sample.value = a. Throughout this book, you will see more typical examples of where a function is called by the event.
Like any language, JScript performs operations. These operations, or methods, manipulate information about objects. In addition to built-in objects such as the String, Math, and Date objects, JScript can also operate on browser objects. This allows you to create new documents and modify your existing forms.
JScript works with browser objects in the same way that it manipulates user objects or built-in objects, which makes the language easier to learn. Most of the code manipulates HTML elements that you already know. For example, it will read properties of a LINK or write information into a TEXTAREA. Use elements you already know about to make pages come alive.
With Microsoft's ActiveX technology, it is also possible to have
JScript manipulate live objects. These operations can give you
multimedia capability, such as sound or graphics, for example.
Additional features can also be added by extending the capability
of the browser with plug-ins or Java applets. These external entities
can also be made to interact with JScript.
Plug-ins are written by software publishers to add capabilities to the browser. These publishers are not required to make these plug-ins work with JScript. So, you must look at the specifications of a plug-in to see whether it supports JScript.
Java and JScript are alike in more than just name. However, there
are significant differences between these two languages. As you
learn to understand the differences, you will also understand
how they can work together. Each has its place and neither does
it all. Table 1.1 provides a quick overview of the differences.
|Interpreted by client||Compiled by the author, run on client|
|Code integrated in HTML documents||Applets distinct from HTML document|
|Loose typing of data types||Strong typing of data types|
|Dynamic binding||Static binding|
|Script limited to browser functions||Stand-alone applications|
|Works with HTML elements||Goes beyond HTML (for example, multimedia)|
|Access browser objects and functionality||Limited access to browser objects or functionality|
Both JScript and Java are languages for building Internet applications. These applications require browsers. The browsers run these applications by reading code embedded in an HTML page. In other words, they both work in the same environment.
Microsoft, Sun, and Netscape have mounted a high-profile campaign to ensure the security of these products. Neither product writes to the user's hard drive. Sensitive information about the user is also unavailable to these languages. So, both products are limited by security and privacy concerns of their environment.
Because the two products have a similar name and work in the same environment, many people do not realize the distinction between JScript and Java.
In addition, it appears that more Internet browsers will support Java than JScript, though this is not certain. They display information differently in a browser window. Java applications can stand alone. One is compiled, the other is interpreted. The development tools are different, and they have a surprisingly different audience.
Java Displays Are Limited to a Graphic Area. To display information on a Web page, Java is limited to painting its text and graphics within a defined area. Just as images on a page are drawn within a defined area of the page, so, too, with Java programs. Within these areas, the Java applets can create animations, paint, and use various fonts. However, an applet cannot affect anything outside its area.
JScript gives you access to the entire Web page. You can modify
properties of the page or any element of the page. You can create
new documents or update parts of a form. Unlike Java, JScript
lets you change the appearance of any part of your Web documents,
not just a limited area.
The hype on Java is that it is flexible enough to do anything, but currently, it cannot affect anything in a Web page outside the area to which it is assigned. If you want your HTML document to interact with Java, forget it. The only way for Java to control everything on the screen is to write a program from scratch and re-create the entire screen. You basically have to rewrite some browser functions.
Directly related to this is Sun's work on a new version of Hot Java, its Web browser. Apparently, the new version's primary goal is to make available general-purpose browser routines for Java programmers. It is not clear at this time how this will play out, but the development of Hot Java is worth watching.
Java Applications Can Stand Alone. Java is a general-purpose language that can create stand-alone applications. Unlike the Java applets that run in Web pages, these applications may not even connect to the Internet, but may perform business functions such as accounting. This is an important aspect of Java that has many people excited.
JScript, like most other scripting languages, works only within
an application. Currently, it works with Microsoft's Internet
Explorer and Netscape's Navigator browsers and the LiveWire server
environment. But JScript applications will not function independently.
Hot Java (not to be confused with Java or JScript) is Sun's own Web browser written in Java. It has shown that Java applications could stand alone. This browser's purpose was to demonstrate the early applets written with the alpha version of Java. With the official release of Java version 1, the original Hot Java is no longer a viable browser. It will not run applets written in Java version 1. A new version of Hot Java is apparently in the works which will support both Java 1.0 applets and JScript.
Java Is a Compiled Language. With Java, you write your code, compile it, and then run it. The person using your Java applet from a Web page cannot look at the source code. For many programmers, there is a sense of security here that you are not giving away your code.
JScript is interpreted. The code you write in JScript is the code that the browser executes. There is no intermediate step of creating executable code from the source code. People can look at the source code of the HTML page and read your JScript code and your comments.
JScript and Java Development Tools. The first generation of development tools for these languages has only recently been introduced. Since JScript and Java are very new languages, this is not surprising. However, looking at the nature of the products, some general distinctions between the development tools can be made.
Java is very much like the C++ language. It is object-oriented, uses many of the same statements, uses libraries, and is compiled. Several companies that have strong C++ programming environments are developing similar environments for Java. This will allow the development of large-scale Java applications, but you will have to learn these programming environments.
JScript is tied to the HTML page. The code is embedded in it and it operates on HTML elements. Since the code is interpreted by a browser, the next generation of HTML editors will undoubtedly add features for creating JScript code.
JScript and Java Have Different Audiences. Some significant features of Java require a multitasking operating system. So, anyone operating on a UNIX platform, OS/2, Windows NT, and Windows 95 will be able to run Java applications and applets. This is a substantial part of the Internet audience.
Learning JScript is easy. By typing in just a few lines, you can be running a JScript application. As you read through this book, you will quickly be incorporating many scripts into your pages. But just as nearly anyone can plant a seed, it does take some patience and skill to create a garden.
One of the more important aspects of the interplay between JScript and Java is the commitment to making the languages work together. They share a similar syntax and control structure that make it easier to write code for either language. But more importantly, a JScript page will be able to communicate with a Java applet referenced by the page.
Another aspect of this sharing takes place not in the browser, but in the server. Both Microsoft and Netscape have server environments that support scripting, so the scripting language that works on the browser will also work on the server. Just as interactive scripts currently run as CGI scripts, JScript can handle such interaction on newer servers that support it.
Several companies are expected either to incorporate JScript into their products or to provide an interface to JScript. The most visible products will be the ActiveX controls and plug-ins as discussed previously. Work is also underway to provide other features, such as accessing high-end databases on the server. It will be interesting to see how this market develops.
All of this holds great potential for creating an exciting Web experience for your viewers. You will be able to use the same language to enhance your Web page, customize your server, create stunning effects with your controls and plug-ins, and communicate with specialized Java applets. JScript can make your Web page come alive; it can make your site an unforgettable experience that your users will want at the top of their favorites list.