[About Javascript][Using the Script tag][Noscript]
[JavaScript entities][Functions][Using the Write method]
[Event handlers][Netscapes handbook]
About Javascript
|
JavaScript is Netscape's cross-platform, object-based scripting language for client and server applications.
There are two types of JavaScript:
Unlike HTML, JavaScript is case sensitive. |
|
The <SCRIPT> tag is an extension to HTML that can enclose any number of JavaScript statements as shown here:
Specifying the JavaScript version
Statements within a <SCRIPT> tag are ignored if the user's browser does not have the level of JavaScript support specified in the LANGUAGE attribute.
If the LANGUAGE attribute is omitted, You can use the LANGUAGE attribute to write scripts that contain Navigator 3.0 features, and these scripts will not cause errors if run under Navigator 2.0. The following examples show some techniques for using the LANGUAGE attribute.
|
|
Only Netscape Navigator versions 2.0 and later recognize JavaScript. To ensure that other browsers ignore JavaScript code,
place the entire script within HTML comment tags, and precede the ending comment tag with a double-slash (//) that indicates
a JavaScript single-line comment:
Although you are not required to use this technique, it is considered good etiquette so that your pages don't generate unformatted script statements for those not using Navigator 2.0 or later.
|
|
The SRC attribute of the <SCRIPT> tag lets you specify a file as the JavaScript source (rather than embedding the JavaScript in the HTML). For example:
The closing </SCRIPT> tag is required. JavaScript statements within a <SCRIPT> tag with a SRC attribute are ignored unless the inclusion has an error. For example, you might want to put the following statement between the <SCRIPT SRC="..."> and </SCRIPT> statements:
External JavaScript files should have the file name suffix
This requirement does not apply if you are using local files.
|
|
Example 1. This example shows how to use two separate versions of a JavaScript document, one for JavaScript
1.0 and one for JavaScript 1.1. The default document that loads is for JavaScript 1.0. If the user is running Navigator 3.0,
the replace method replaces the page.
|
Noscript
Specifying alternate content
|
Use the <NOSCRIPT> tag to specify alternate content for browsers that do not support JavaScript.
HTML enclosed within a <NOSCRIPT> tag is displayed by browsers that do not support JavaScript;
code within the tag is ignored by Navigator.
Note however, that if the user has disabled JavaScript by choosing Network Preferences from the Options menu, Navigator displays the code within the <NOSCRIPT> tag. The following example shows a <NOSCRIPT> tag.
|

JavaScript entities
Using JavaScript expressions
as HTML attribute values
|
Using JavaScript entities, you can specify a JavaScript expression as the value for an HTML attribute. Entity values are evaluated dynamically. This allows you to create more flexible HTML constructs, because the attributes of one HTML element can depend on information about elements placed previously on the page.
You may already be familiar with HTML character entities by which you can define characters with special numerical codes or names by preceding the name with an ampersand (&) and terminating it with a semicolon (;). For example, you can include a greater-than symbol (>) with the character entity & JavaScript entities also start with an ampersand (&) and end with a semicolon (;). Instead of a name or number, you use a JavaScript expression enclosed in curly braces {}. You can use JavaScript entities only where an HTML attribute value would normally go. For example, suppose you define a variable barWidth. You could create a horizontal rule with the specified percentage width as follows:
As with other HTML, after layout has occurred, the display of a page can change only if you reload the page.
Unlike regular entities which can appear anywhere in the HTML text flow, JavaScript entities will be interpreted
only on the right-hand side of HTML attribute name/value pairs. For example,
|

Defining and calling functions
|
Functions are one of the fundamental building blocks in JavaScript. A function is a JavaScript procedure
--a set of statements that performs a specific task. A function definition has these basic parts:
Generally, you should define the functions for a page in the HEAD portion of a document. That way, all functions are defined before any content is displayed. Otherwise, the user might perform an action while the page is still loading that triggers an event handler and calls an undefined function, leading to an error. The following example defines a simple function in the HEAD of a document and then calls it in the BODY of the document:
The square function used the line
In addition to defining functions as described here, you can also define Function objects. A method is a function associated with an object. |
|
In a script you can do all kinds of things you can't do with ordinary HTML. For example, you can display text conditionally or based on variable arguments. For these reasons, write is one of the most often-used JavaScript methods. The write method takes any number of string arguments that can be string literals or variables. You can also use the string concatenation operator (+) to create one string from several when using write. Consider the following script, which generates dynamic HTML with Navigator JavaScript:
|
To see this output go here
|
|
The HEAD of this document defines two functions:
The following line creates the output of the bar function:
|
|
Navigator 3.0 prints output created with JavaScript. To print output, the user chooses Print from the File menu. To view HTML code that was generated with JavaScript write and writeln methods, the user must specify the view-source: protocol. If the user chooses Document Source or Frame Source from the View menu, the content displayed is that of the wysiwyg: URL. The following example shows a view-source: URL: |
|
|
Navigator 2.0, does not print output created with JavaScript. For example, if the user chooses
Print from the File menu to print the page in the previous example, you see only the line that reads
"This is some standard HTML...," even though you see both lines onscreen.
|
|
JavaScript in Navigator generates its results from the top of the page down. Once text has been displayed, you cannot change it without reloading the page. In general, you cannot update part of a page without updating the entire page. However, you can update
|
|
JavaScript applications in the Navigator are largely event-driven. Events are actions that occur usually as a result of something the user does. For example, clicking a button is an event, as is changing a text field or moving the mouse over a hyperlink. You can define event handlers, such as onChange and onClick, to make your script react to events. Each event is recognized by certain objects (HTML tags), summarized in the following table:
If an event applies to an HTML tag, then you can define an event handler for it. The name of an event handler is the name of the event, preceded by "on." For example, the event handler for the focus event is onFocus. To create an event handler for an HTML tag, add an event handler attribute to the tag; Put JavaScript code in quotation marks as the attribute value. The general syntax is
For example, suppose you have created a JavaScript function called compute. You can cause Navigator to perform this function when the user clicks a button by assigning the function call to the button's onClick event handler:
Notice in the preceding example In general, it is good practice to define functions for your event handlers:
|
|
Use single quotation marks ( ' ) to delimit string literals so that scripts can distinguish the literal from attribute values enclosed in double quotation marks. In the following example, the function bar contains the literal "left" within a double-quoted attribute value:
|
|
In the following example, you can enter an expression (for example, 2 + 2) in the first text field, and then click the button. The second text field then displays the value of the expression (for example, 4).
The HEAD of the document defines a single function, compute, taking one argument, f, which is a Form object. The function uses the Navigator JavaScript method confirm to display a Confirm dialog box with OK and Cancel buttons.
If the user clicks OK, then confirm returns true, and the value of the result text field is set to the value of If the user clicks Cancel, then confirm returns false and the alert method displays another message.
The form contains a button with an onClick event handler that calls the compute function. When the user clicks the button, JavaScript calls compute with the argument
|
| To find more information about Javascript and how to use it, go to Netscapes Javascript Guide. |

[Page][Font][Text Style ][Image][Table][Form][Applet][Frames]
[Marquee][Image Map][Javascript][Entities][Tag Index]