Basics
Using JavaScript in HTMLJavaScript can be embedded in an HTML document in two ways:
The SCRIPT tag
A script embedded in HTML with the SCRIPT tag uses the format:
The optional LANGUAGE
attribute specifies the scripting language as follows:
The HMTL tag, <SCRIPT>, and its closing counterpart, </SCRIPT> can enclose any number of JavaScript statements in a document.
JavaScript is case sensitive.
Example 1 page display.
Hello guys !! That's all folks.
Defining and Calling Functions
Scripts placed within SCRIPT tags are evaluated after the page loads. Functions are stored, but not executed. Functions are executed by events in the page.
It's important to understand the difference between defining a function and calling the function. Defining the function simply names the function and specifies what to do when the function is called. Calling the function actually performs the specified actions with the indicated parameters.
All done.
Thanks.
Example 3 results.
Thanks.
Quotes
Use single quotes (') to delimit string literals so that scripts can distinguish the literal from attribute values enclosed in double quotes. In the previous example, function bar contains the literal 'left' within a double-quoted attribute value. Here's another example:
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, a button click is an event, as is giving focus to a form element. There is a specific set of events that Navigator recognizes. You can define Event handlers, scripts that are automatically executed when an event occurs.
Event handlers
are embedded in documents as attributes of HTML tags to which you assign JavaScript
code to execute. 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 on a button by assigning
the function call to the button's onClick event handler:
You can put any JavaScript statements inside the quotes following onClick. These statements get executed when the user clicks on the button. If you want to include more than one statement, separate statements with a semicolon (;).
In general, it is a good idea to define functions for your event handlers because:
Notice in this example the use of this.form to refer to the current form. The keyword this refers to the current object-in the above example, the button object. The construct this.form then refers to the form containing the button. In the above example, the onClick event handler is a call to the compute() function, with this.form, the current form, as the parameter to the function.
Events apply to HTML tags as follows:
If an event applies to an HTML tag, then you can define an event handler for it. In general, an event handler has the name of the event, preceded by "on." For example, the event handler for the Focus event is onFocus.
Many objects also have methods that emulate events. For example, button has a click method that emulates the button being clicked. Note: The event-emulation methods do not trigger event-handlers. So, for example, the click method does not trigger an onClick event-handler. However, you can always call an event-handler directly (for example, you can call onClick explicitly in a script).
| Event | Occurs when... | Event Handler |
|---|---|---|
| blur | User removes input focus from form element | onBlur |
| click | User clicks on form element or link | onClick |
| change | User changes value of text, textarea, or select element | onChange |
| focus | User gives form element input focus | onFocus |
| load | User loads the page in the Navigator | onLoad |
| mouseover | User moves mouse pointer over a link or anchor | onMouseOver |
| select | User selects form element's input field | onSelect |
| submit | User submits a form | onSubmit |
| unload | User exits the page | onUnload |
Example 4: a script
with a form and an event handler attribute.
Example 4 page display.
Example 5: a script
with a form and event handler attribute within a BODY tag.
Example 5 page display.
Enter a number in the field and then click your mouse anywhere OUTSIDE of the field. Depending on what you enter, you will be prompted to enter another number, or thanked.
This section describes various useful scripting techniques.
Updating Pages
JavaScript in Navigator generates its results from the top of the page down. Once something has been formatted, you can't change it without reloading the page. Currently, you cannot update a particular part of a page without updating the entire page. However, you can update a "sub-window" in a frame separately.
Printing You cannot
currently print output created with JavaScript. For example, if you had the
following in a page:
This is some text. And you printed it, you would get only "This is some text", even though you would see both lines on-screen.
Using Quotes Be sure
to alternate double quotes with single quotes. Since event handlers in HTML
must be enclosed in quotes, you must use single quotes to delimit arguments.
For example
Alternatively, you can escape quotes by preceding them by a backslash (\).
Defining FunctionsIt is always a good idea to define all of your functions in the HEAD of your HTML page. This way, all functions will be defined before any content is displayed. Otherwise, the user might perform some action while the page is still loading that triggers an event handler and calls an undefined function, leading to an error.