Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Hiding Your Scripts You will probably be designing pages that may be seen by browsers that don’t support JavaScript. To keep those browsers from interpreting your JavaScript commands as HTML—and displaying them—wrap your scripts as follows:

<SCRIPT LANGUAGE=”JavaScript”>
<!-- This line opens an HTML comment
document.write(“You can see this script’s output, but not its source.”)
//   This is a JavaScript comment that also closes the comment -->
</SCRIPT>

The opening <!-- comment causes Web browsers that do not support JavaScript to disregard all text they encounter until they find a matching --> therefore, they don’t display your script. You do have to be careful with the <SCRIPT> tag, however; if you put your <SCRIPT> and </SCRIPT> block inside the comments, the Web browser ignores them also.

Comments Including comments in your programs to explain what they do is usually good practice—JavaScript is no exception. The JavaScript interpreter ignores any text marked as comments; therefore, don’t be shy about including them. You can use two types of comments: single-line and multiple-line.

Single-line comments start with two slashes (//) and are limited to one line. Multiple-line comments must start with /* on the first line and end with */ on the last line. A few examples are

   // this is a legal comment
/ illegal -- comments start with two slashes
/* Multiple-line comments can
   be spread across more than one line, as long as they end. */
/* careful -- this comment doesn’t have an end!
/// this comment’s OK because extra slashes are ignored //


CAUTION:  

Be careful when using multiple-line comments—remember that these comments don’t nest. If you commented out a section of code in the following way, for example, you would get an error message:

/* Comment out the following code
 * document.writeln(DumpURL()) /* write out URL list */
 * document.writeln(“End of list.”)
 */

To avoid error messages, the preferred way to create single-line comments is as follows:

/* Comment out the following code
 * document.writeln(DumpURL()) // write out URL list
 * document.writeln(“End of list.”)
 */

Using <NOSCRIPT> You can improve the compatibility of your JavaScript Web pages through the use of the <NOSCRIPT>…</NOSCRIPT> HTML tags. Any HTML code placed between these container tags will not display on a JavaScript-compatible Web browser but will display on one that cannot understand JavaScript. This enables you to include alternative content for your users who are using Web browsers that don’t understand JavaScript. At the very least, you can let them know that they are missing something, as in this example:

<NOSCRIPT>
<HR>If you are seeing this text, then your web browser
   doesn’t speak JavaScript!<HR>
</NOSCRIPT>

The JavaScript Language

JavaScript was designed to resemble Java, which, in turn, looks a lot like C and C++. The difference is that Java was built as a general-purpose object language; JavaScript, on the other hand, is intended to provide a quicker and simpler language for enhancing Web pages and servers. This section describes the building blocks of JavaScript and teaches you how to combine them into legal JavaScript programs.


NOTE:   You can find a complete language reference for JavaScript in Appendix A, “JavaScript 1.2 Language Reference,” and on the CD-ROM that accompanies this book. You can also find this information and more at Netscape’s DevEdge Online Web site at http://developer. netscape.com.

Using Identifiers

An identifier is a unique name that JavaScript uses to identify a variable, method, or object in your program. As with other programming languages, JavaScript imposes some rules on what names you can use. All JavaScript names must start with a letter or the underscore character; they can contain both upper and lowercase letters and the digits 0 through 9. JavaScript supports two ways for you to represent values in your scripts: literals and variables. As their names imply, literals are fixed values that don’t change while the script is executing, and variables hold data that can change at any time.

Literals and variables have several types; the type is determined by the kind of data that the literal or variable contains. The following are some of the types supported in JavaScript:

  Integers—Integer literals are made up of a sequence of digits only; integer variables can contain any whole-number value. You can specify octal (base-8) and hexadecimal (base- 16) integers by prefixing them with a leading 0 or 0x, respectively.
  Floating-point numbers—The number 10 is an integer, but 10.5 is a floating-point number. Floating-point literals can be positive or negative and can contain either positive or negative exponents (which are indicated by an “e” in the number). For example, 3.14159265 is a floating-point literal, as is 6.023e23 (6.023×1023, or Avogadro’s number).
  Strings—Strings can represent words, phrases, or data and are set off by either double () or single () quotation marks. If you start a string with one type of quotation mark, you must close it with the same type. Special characters, such as \n for newline and \t, can also be utilized in strings.
  Booleans—Boolean literals can have values of either TRUE or FALSE; other statements in the JavaScript language can return Boolean values.

Using Functions, Objects, and Properties

JavaScript is modeled after Java, an object-oriented language. An object is a collection of data and functions that have been grouped together. A function is a piece of code that plays a sound, calculates an equation, sends a piece of email, and so on. The object’s functions are called methods and its data are called its properties. The JavaScript programs you write will have properties and methods and will interact with objects provided by the Web browser, its plug-ins, Java applets, ActiveX Controls, and other things.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.