Writing scripts

Unit 3

JavaScript Samples

Look at the samples, and view their source documents.

External file sample
The JavaScript file   sample-1-4-external-file.js   contains:
document.write("hello");

Sample with an external file in the head, onclick, and pseudo-protocol
The JavaScript file   sample-3-2-head-and-onclick.js   contains:
var cat = "Cleos page";
function showCat() { return this.cat; }
function setCat(cat_parm) { this.cat = cat_parm; }

The html element:
<a href="javascript: showCat()">Show cat</a>
uses the Javascript: pseudo-protocol.
This creates a page with the text returned from the showCat() function.
The word this in the functions refers to to the current object, which is the current window. Both functions use the cat attribute, which the code added to the window object. This is interesting; the code specified var cat ; that made cat an attribute of the window object.

Sample 3-2 is very interesting. It shows we can build the entire content of a page entirely with JavaScript. More often, we set the page up using xhtml and add more content to the page using JavaScript.

Firefox toolbar

There is a Firefox toolbar, which helps in developing JavaScript.

Lab 1 part C

In lab 1 part C you will build one more page. It will contain JavaScript.

Put your web page on the Internet

Put your page on the internet, as you did with lab 1B.

Test your page

Validate your page before trying to get the JavaScript to work. Then make sure the JavaScript works.

Complete lab 1 part C

You have done all the work for part C of lab 1.
Now, send note to instructor.

Reading assignment

Reading assignments are in the text book, Java Script, A Beginner's Guide, Second Edition, by John Pollock; McGraw Hill / Osborne, ISBN 0-07-222790-7

Read Module 2. We will not use the projects in the book; we will have our own lab assignments. You may wish to do the Mastery Check at the end of each module; the answers are in the appendix.
The author of the book uses HTML; we will use XHTML, so be careful to upgrade to XHTML when you copy anything from the book.

Case rules:

HTML
HTML does not care whether you use upper or lower case. We will NOT be using HTML.
XHTML
The names of elements and attributes must be in lower case.
JavaScript
JavaScript is case sensitive. When you use lower case letters in a name, make sure you use lower case everywhere for that name. When you use upper case letters in a name, make sure you use upper case everywhere for that name. For example: timeForABreak is a valid name; make sure the correct letters are upper case.

Skip the section titled "Hiding JavaScript Code from Older Browsers". This technique can cause some problems. It is better to put the JavaScript in an external file. Do not use <--     //--> to try to hide JavaScript.

Alternate reading assignments are in the text book, Java Script Concepts & Techniques Programming Interactive Web Sites, by Tina Spain McDuffie; Franklin, Beedle & Associates, ISBN 1-887902-45-7

Read Chapter 2. Omit the section on Hiding Scripts from Old Browsers, which is becomming obsolete. Omit the section on Introducing the navigator Object, which we will do later.

Lecture notes

Do NOT read the lecture notes before hearing the lecture. If you do, you will find the lecture very boring. Read the lecture notes if you do not attend the lecture, or if you wish to review the material.