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

CS-90.3 Assignment 2

Introduction

Doing JavaScript “right” includes minimizing the number of global variables used in your code. This assignment is one step in developing a framework for your JavaScript code that eliminates all global variables in your code while reducing the total number of global variables to one: the Core object created by the core library library given in the textbook.

The Assignment

  1. Install the Core library and link to it from your site’s index page.

    Use the link in the Introduction to download a copy of the core library, and put it in the scripts directory of your website. Using the same index.xhtml file you used for the first assignment, put a link to this library in the head of your document. Be sure the link to the library is above the link to your own JavaScript code. You will be editing the script file from Assignment 1 for this assignment; there is no need to rename it or to create a new file.

  2. Use Core.start() to set up two event listeners for the onload event.

    Remove the existing code from sample.js (or whatever you might have renamed it in Assignment 1), and create two global objects named obj1 and obj2. Add a function named init() to both objects: the function in obj1 is to display an alert that says, “Hello from obj1,” and the function in obj2 is to display an alert that says, “Hello from obj2.”

    Add two calls to Core.start(): pass obj1 as an argument to the first one, and obj2 to the second one.

    Test your code. If you have done things correctly, you should first see the content of your web page, and then see the two alert messages, one after the other. (Remember that in Assignment 1 you saw the alert message before the page content appeared.) Try viewing your web site using all the browsers in the lab: Firefox (of course!), Opera, Internet Explorer, and Safari. You might also want to download and install Google Chrome and try that browser too. (We have not been able to install a copy of Chrome that all users can share the way other programs do; each person seems to need to install their own copy.)

    In the body of your email message, when you submit the assignment, tell the sequence of the two alert messages, and whether there was any difference across browsers.

  3. Eliminate the global variables.

    You can eliminate the two global variables (obj1 and obj2) by creating anonymous (unnamed) objects to pass to Core.start(), as demonstrated in class on September 22:

    Core.start( { init: function() { alert("Hello from obj1"); } } );

    But this all–on–one–line style will not work when you write real JavaScript applications; there will be too much code to fit on one line, and the code will be impossible to read. Reformat the two calls to Core.start() so the proper indentation is given:

    Core.start ( // the object goes here );

    Test your code to be sure it still works. (No need to run it in the other browsers unless you want to.) Be sure your code validates with no warnings and no errors when you click on the XHTML link at the bottom of the page.

Submit The Assignment

When you have tested your web site and verified that everything validates and runs correctly, log out so your site will be copied back to the server. Then send me an email to let me know your assignment is ready for grading: I will copy your site from the lab server to babbage to verify that it is correct.

Send your email to vickeryatbabbage.cs.qc.cuny.edu with CS-90.3 Assignment 2, spelled and capitalized just like that. Be sure to put your email address inside the message you send.