"); } else { $mime_type = "text/html"; header("Content-type: $mime_type; charset=utf-8"); } ?> CS-90.3 Assignment 5

CS-90.3 Assignment 5

Overview and Preparation

For this assignment, you are to implement a web page that uses JavaScript event handlers to modify the appearance of XHTML elements in response to user-generated events. Specifically, the assignment is to change the color of the text of some paragraphs as the user moves the mouse over them.

Set up your index page so that it contains at least 5 paragraphs of Latin text generated by the lipsum.com web site. That web site explains the rationale for placing “placeholder” text on a web page. After you read about that, use the form in the lower right corner to pick the number of paragraphs you want, and then click the “Generate Lorem Ipsum” button. Select the Latin paragraphs from the generated web page, and paste them into your site’s index.xhtml file, between the h1 element and the footer div at the bottom of the page.

Note that the paragraphs you paste into your document will not be actual XHTML paragraph elements; you will need to wrap each one in p and /p tags. In Dreamweaver, the keyboard shortcut to do this is to select the text in a paragraph and to type Ctrl-Shift-P.

Wrap one word in the h1 element and one word in the first paragraph in span and /spantags.

Create a stylesheet named assignment_05.css in your site’s css directory. Add a rule to this style sheet so that all span elements will use white text when the user moves the mouse over them. (The selector is span:hover.)

Check your web page:

The Assignment

I am providing you with an outline for the JavaScript code for the assignment. Once you understand the outline, you are to extend it to implement the features listed in the Assignment Steps.

Replace all links to scripts in your index.xhtml page with a new link to a single script file named scripts/assignment_05.js. Create that file, and copy the following code into it:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
window.onload = function() { function mouseoverHandler(evt) { console.log('mouseover: ' + this.which_one); } var paragraphs = document.getElementsByTagName('p'); console.log("There are " + paragraphs.length + " paragraphs."); for (var i = 0; i < paragraphs.length; i++) { paragraphs[i].which_one = i; paragraphs[i].onmouseover = mouseoverHandler; } };

Code Notes

Assignment Steps

  1. Be sure the code provided to you works, and that you understand it.

    Run the code with the JavaScript console open, and see that a “mouseover: n” message is displayed each time you move your mouse over a paragraph, and that the n in the message is a number that corresponds to which paragraph you moved the mouse over. Be sure the code displays another message every time you move your mouse over each paragraph.

  2. Add a mouseout handler to each paragraph.

    Use the code given to you as a model, and add another function, named mouseoutHandler, to the project. Have it display a message that says “mouseout: n” like the one the mouseover listener displays. Add the line of code to the loop that set up this function as the onmouseout event handler for each paragraph.

    Test the code, and make sure moving the mouse over a paragraph causes one message to be displayed and that moving the mouse away from the paragraph causes the complementary message to be displayed. Be sure you get the proper sequence of messages no matter which paragraph you move the mouse over and no matter how many times you move over different paragraphs.

  3. Change the text color to red when the mouse moves over a paragraph, and back to black when the mouse moves away from the paragraph.

    Here is the line of code you need to add to the mouseover event handler to make the text turn red:

    this.style.color = 'rgb(255, 0, 0)';

    You could use any CSS representation of the color for the string ('red', #F00, etc); the representation given here is what browsers use internally.

    Use similar code in the mouseout handler to make the text color turn back to black (rgb(0, 0, 0)) when the mouse moves away from it.

    Check your code to make sure the text color changes to red whenever you move the mouse over it, and back to black when you move the mouse away from it.

    Note that all this JavaScript code does exactly the same thing you could have accomplished with a single CSS rule: p:hover { color: red; }. If this was all you wanted to accomplish, there would be no reason to use JavaScript!

  4. Modfy the page so each paragraph cycles through three colors.

    The first time the user moves the mouse over a paragraph, it turns red. The next time, it turns green; the next time, it turns blue; the next time, it turns red again; etc.

    If you were to remove the code that turns each paragraph black when the mouse leaves it, you could see what the current color is when the mouse enters the paragraph, and change the color to the next one. But having the the paragraphs turn black when the mouse leaves them means the current color will always be black when the mouse enters them again. The solution is to add your own property to each paragraph, where you would keep track of the last non-black color applied to it.

    First look at the logic your mouseover handler could use. In computer science, the logical steps a program follows are called, its algorithm. Here is the algorithm using English phrases in place of some of the actual JavaScript code, a form called “pseudocode:”

    if (the last color was red) { set the color to green } else if (the last color was green) { set the color to blue } else { set the color to red }

    You are to turn this pseudocode into valid JavaScript. Since you are to restore each paragraph to black when the mouse exits, you will have to use your own property to record what the last color was (instead of using this.style.color to find out what the current color is). The sample code is an example of adding your own property (which_one) to paragraphs. In this case, you can use simple strings, like 'red', 'green', and 'blue' for your property values. You could even use simple letters ('r', 'g', 'b') or even arbitrary numbers (1, 2, 3) to represent the previous color. Every time you set the color of a paragraph, record which color it is in your personal property for the paragraph.

    In class, I showed how to use a JavaScript switch statement intead of a lot of if and else if keywords to keep track of the last color a paragraph was. You may use that code for this assignment, or you can use a more complicated (but easier to understand) if else structure if you prefer.

    Note that the which_one property name is not appropriate for the actual code in this assignment. It was used above only to demonstrate how you can add your own property to a DOM object. For the assignment, you should use a more meaningful property name, like last_color that I used in my code in class.

Submit

There are no questions to answer for this assignment. When you are sure your program does everything correctly, just send me an email message, and I’ll check it out in the usual way.

Send your email message to me at: vickeryatbabbage.cs.qc.cuny.edu by midnight of the due date.

The Subject line of your email message must be: “CS-90.3 Assignment 5.”

Don’t forget to put your name in your email message!