A Few Words about Scripting

[ LiB ]

A Few Words about Scripting

Perhaps you are wondering what the difference is between scripting and pro gramming . The answer is nothing. They are the same thing. Many people like to think of scripts as small units of program code that perform a specific function and of programs as larger, more robust code. As you will soon learn, scripts can be written that fulfill both of these definitions.

One possible way to differentiate a script from a program is to define scripts as interpreted programs that are processed one line at a time and to define programs as collections of code that must be compiled before they can execute. Compiling is the process of converting a program's instructions into machine language so that the computer can process them. Because JavaScript and JScript are interpreted languages , they need something to interpret the code as they execute. This is the job of the Internet browser and the WSH, which also provide scripts with a programming environment complete with environment-specific object models that provide JavaScripts with access to resources such as windows and browser history information and provide JScript with access to files, folders, drives , and so on.

I know that I keep mentioning objects and object models; I will continue to do so throughout the morning before I finally provide detailed coverage later this morning and in the first part of the afternoon. Learning a new programming language is not always easy and is made only more difficult by the fact that just about every new concept depends on your knowing another one. Unfortunately, I can only teach you one concept at a time. So hang in there!

Getting Comfortable with Statement Syntax

JavaScript and JScript are case sensitive. This means that you must be careful to use correct spelling and capitalization when typing script statements. For example, in a few minutes you are going to learn how to work with variables . If you create a variable in the beginning of your script with the name total-Value , you must use this exact spelling throughout your script. Change a single element of spelling and you will get an error because JavaScript and JScript will think that you are referencing a different variable.

NOTE

TIP

If you are like me,you will never be able to remember the correct spelling of every JavaScript language element.That means you'll be checking Appendix A,"A Brief JavaScript and JScript Reference," a lot.This appendix provides a list of JavaScript objects, properties, and methods . If you are ever in doubt about how to spell any of these elements, check the appendix first. It will save you a lot of time and frustration.

With the exception of its case-sensitivity requirement, JavaScript and JScript are very easygoing languages in that they do not impose a strict set of rules about how you should format your scripts. Statements can begin on one line and end on the next , or you can place multiple statements on a single line by typing a semicolon ( ; ) at the end of each statement. The semicolon tells JavaScript and JScript explicitly where a statement ends. However, packing more than one statement on a single line of code can get ugly if you do it too much.

NOTE

TIP

Although the ; is not required for any JavaScript statement, it's a good idea to get into the habit of ending all your statements with one. It makes your script easier for the next person to read and work with.

Hiding JavaScript Statements

HTML comments begin with the characters <!-- and end with --> . The browser knows not to display anything between these sets of characters. You also know that the HTML <SCRIPT> tag is used to embed JavaScript into HTML pages. Both JavaScript and non-JavaScript browsers know not to display the <SCRIPT> tags.

However, non-JavaScript browsers do not know what to make of the statements within the <SCRIPT> tags, so they will display the actual lines of the script right in the middle of the HTML page. I am sure that this is not what you want your visitors to see. To get around this unsightly mess, you can enclose all the JavaScript statements starting after the beginning <SCRIPT> tag and before the ending </SCRIPT> tag within HTML comments, as demonstrated in the following example:

 <HTML>   <HEAD>     <TITLE>Script 2.1 -Example of Comment Statements</TITLE>   </HEAD>   <BODY>     <SCRIPT LANGUAGE="JavaScript" TYPE="Text/JavaScript">  <!-- Start hiding JavaScript statements  document.write("Non-JavaScript browsers will not see this message.");  // End hiding JavaScript statements -->  </SCRIPT>   </BODY> </HTML> 

Since browsers are smart enough not to process any HTML that they do not understand, the starting and ending <SCRIPT> tags are ignored by non-JavaScript browsers. At the same time, non-JavaScript browsers will view everything between the HTML comments tags as one big comment and ignore it, thus hiding all JavaScript statements and preventing their display.

JavaScript-enabled browsers, on the other hand, will recognize the starting and ending <SCRIPT> tags and process them accordingly . These browsers also will recognize the opening HTML comment tag and ignore it, while simultaneously seeing the line beginning with the // characters as a JavaScript comment and ignoring it as well.

NOTE

TIP

You also can use comments to make your JavaScripts and JScripts self-documenting by using them to add descriptive comments to your scripts.

As long as you model your scripts after this example, you'll trick old browsers into thinking that your JavaScript statements are just HTML comments, without affecting what JavaScript-aware browsers see.

[ LiB ]


Learn JavaScript In a Weekend
Learn JavaScript In a Weekend, Second Edition
ISBN: 159200086X
EAN: 2147483647
Year: 2003
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net