Exercise Two

In this exercise, you will copy out pre-written functions, then call those functions from a variety of links and INPUT tags; this will help you to become more comfortable with the mechanics of JavaScript programming.

1. Create a new HTML page. Place a SCRIPT tag in the HEAD of your page which contains the following functions:

function alertAndOpenWindow(myMSG, myURL) {
    alert(myMSG);
    window.open(myURL, "new_window", "width=300,height=200");
}

function alertMe(myMSG) {
    alert(myMSG);
}

function alertDefaultMessage() {
    alert("There is no message at this time.");
}

Note: Please do NOT cut-and-paste these functions from this page. The point of this exercise is to make you type out everything yourself, to make you become more accustomed to the JavaScript function syntax. If you cheat, you won't learn anything from this exercise!

2. Now, in the BODY of your HTML page, make one link which calls the alertDefaultMessage() function. Test your page; make corrections, as needed.

3. Make two more links, which both call the alertMe() function; one link should do so using the onClick event handler, the other should do so using the javascript absolute URL. The first link should pass the string, "Wowza", as an argument to the function; the second link should pass the string, "Hullabaloo", as an argument to the function. Test your page; make corrections, as needed.

4. Add a form with two button INPUT tags in it; the value of the first button should be "Yahoo", and the value of the second should be "AltaVista". Call the alertAndOpenWindow() function from each of these buttons using the onClick event handler. From the "Yahoo" button's onClick event handler, pass the string, "We are now going to Yahoo" as the first argument to the function, and the string, "http://www.yahoo.com/" as the second argument to the function. Test this button; make corrections, as needed.

5. From the "AltaVista" button's onClick event handler, pass the string, "We are now going to AltaVista" as the first argument to the function, and the string, "http://www.altavista.com/" as the second argument to the function. Test this button; make corrections, as needed.

View Solution Page.

Main Menu