cmpt165

Spring 2007

CMPT 165-D2 @ Surrey: Lab 6

The objective of this lab is to start familiarizing you with JavaScript programming, and adding JavaScript to your HTML pages.

  1. Begin this lab by downloading this XHTML file. This file contains the necessary tags to load in your JavaScript file. Make sure you recognize and understand what they are.

    As you've been shown in class, you are going to create a separate file, called lab6.js, where you will write your JavaScript program. You can use Notepad to create this file.

  2. In your JavaScript file, add the following line: alert("Hello World!");

    Next upload your XHTML page and your JavaScript file to the course web server. Load your page and see what happens.

  3. Next, modify your JavaScript program so that it now creates two variables, var1 and var2, and then adds them together. Instead of outputing "Hello World!", this time output the result of adding var1 and var2 together.

  4. In class you were given several examples of JavaScript code. Once you have tried the above, now try out some of the examples and see if they work. (Note that if you don't have your course notes with you, you can download them from WebCT.)

  5. Finally, in class we looked at the if statement and the for loop. Recall that the for-loop syntax: for (initializer; test; increment). (E.g. for (var i = 0; i < 5; i++) { ... } ). By modiying your existing JavaScript file, lab6.js, create your own for-loop that adds the numbers from 1 to 10 and ouputs the result using alert. Upload this new JavaScript file to the course server and reload your XHTML page once you are done.

    When you're done, show your latest XHTML and JavaScript files to your lab TA.

  6. Please note that you are expected to keep a copy of the work that you have done. You can keep a record on the student server (SIAT), a portable hard drive (e.g. key drive), or simply by email the URL you have created to yourself.