|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Hiding Your Scripts You will probably be designing pages that may be seen by browsers that dont support JavaScript. To keep those browsers from interpreting your JavaScript commands as HTMLand displaying themwrap your scripts as follows: <SCRIPT LANGUAGE=JavaScript> <!-- This line opens an HTML comment document.write(You can see this scripts 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 dont 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 practiceJavaScript is no exception. The JavaScript interpreter ignores any text marked as comments; therefore, dont 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 doesnt have an end! /// this comments OK because extra slashes are ignored //
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 dont 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 doesnt speak JavaScript!<HR> </NOSCRIPT> The JavaScript LanguageJavaScript 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.
Using IdentifiersAn 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 dont 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:
Using Functions, Objects, and PropertiesJavaScript 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 objects 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.
|
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. |