Thursday, November 30, 2006

week 10: 11/17/06

Hope everyone had a nice break last week for Thanksgiving (and from this class). I for one could use 4-day weekends a little bit more often. In any case, I hope you all haven't gone the full two weeks away from working on your final projects. There are about 3 weeks remaining before they are due, so please don't put things off. In general, I've so far been happy with the the class's performance this term, and I think most of you do get the subject matter rather well when you spend a little time with it. Therefore, I have high hopes of seeing some nice projects come the end of the term.

During our last class, we all but completed our work with form validation, in which we went over the form validation scripts that we began for the text fields. We then we began work on validating checkboxes. We have one last step to complete on those checkboxes in our next class, and then we will proceed on to validating radio buttons.

From there, we will move on to a new script for image replacement, where images change when the mouse moves over them, and again when the mouse moves off. We will combine that script with pop-up windows.

All of this we will cover in the next 2 to 2 1/2 weeks, leaving us at least a half-day in class to work on your final projects, due in 3 weeks. This all means that we have a lot of work ahead of us, so please please please do not fall behind on your final projects. I can usually stay late after class if any of you wish to discuss your projects with me; however, I'm not usually inclined to do so if you haven't done any work on it beforehand. So, do some work on your projects and I'll stay until the school closes with you working if need be. Carter-

Sunday, November 12, 2006

week 9: 11/10/06

Hi everybody, it seems that blogger.com is having issues today, and as a result I haven't been able to post any files for the final project. It should be up tonight late or tomorrow morning depending on if their server becomes available in the next couple of hours

Whatever the situation, you should keep checking back here over the next day or so to see if it is up. Moreover, I'll make a full posting for the last class during the week.

Please let me know if you have any other questions or problems. Carter-


Okay then, it seems that this morning things are on the blogger site are going much quicker and that I will be able to upload the files I need so that you all can have them for your final projects. So, let's get right to it. Your final project consists of a small web-site of six (6) pages of content. This does not mean six html files, no, but six pages OF CONTENT. There may be other files that you will produce that do not contain any content, such as frameset files.

  1. TOPICS:
    • Final Project;
      • LINK   Content Requirements;
      • LINK   Technical Requirements;
      • LINK   Design Requirements;

    • Form Validation;
      • LINK   Creating the Table;
      • LINK   Adding the Form;
      • LINK   Designing the Layer;
      • LINK   Adding the IDs;
      • LINK   Altering Properties;
      • LINK   Using a Conditional;
      • LINK   Submitting Completed Form;


  2. HOMEWORKFinal Project, Part I: Starting this week, the homework for each class is part of the final project. It is not extraneous to what you will be required to do and what will be due the last day of class. If you produce your homework each week, you will be largely finished with the project well before it is due.
        Part I, due the next class (11/17/06⁙week 10), consists of two things:

    • A Logo—Create a new logo for the site described below. This will be used on the intro page as well as at the top of every other page in the site. It must follow the same complimentary color scheme described below.
    • A Color Scheme—You must decide upon your color scheme for your site by Friday. This does not mean it cannot change somewhat over the course of your work during the next 5 weeks; but rather, it is meant as a initial design decision as a way for you to begin to determine the direction it will take.
          LINK
      View the CSS code of the following document at this link. The code has directions within it instructing you what to do. The link takes you to the document you will use to demonstrate your color scheme to me. Once you have clicked on the link, choose view << source to see the code. All you need to look at is the top of the document in the CSS. Please send me an email, or ask me before Friday's class if you are unclear what I want.


  3. FINAL PROJECT: Democracy Now! website Re-Design The current Democracy Now! website is found here (LINK), and you should look through the site and familiarize yourself with its topic and mission. Knowing what the site is about is imperative to making design decisions for color and layout. Your project is to re-design the parts of the site detailed below.
    1. CONTENT REQUIREMENTS: 
      1. Page 1: INTRO PAGE—This page has no requirements other than the technical and design requirements for the entire site (see below: LINK). All it consists of is some sort of graphic introduction, an image, or flash, or whatever you prefer to introduce us to the site, and a link to the primary frameset (index) of the site. It contains NO OTHER CONTENT.
      2. Page 2: HEADLINES PAGE—This page includes all the headlines, summaries, images and captions which I will provide to you. If you wish to add any additional headlines and images to this page, please do so. The images and text required for this page will be given to you near the end of the term. We will save this page for last so that the headlines are as current as possible.
      3. Page 3: MAIN PAGE—This page must have the introductory text, About Democracy Now! that I will provide you, as well as a couple of images that I will also provide to you. Any other text or images that you find and wish to include is welcomed and encouraged; however, it must remain on the topic of the site.
            You must use all the information on the following two (2) pages in your main page found here:
            LINK (about)
        and here:
            LINK (history)
        The images required for these pages are here:
            LINK

      4. Page 4: IMAGE GALLERY PAGE—This page must have ALL the images and caption text that I will provide you. There will be at least ten (10) images. Once again, please use additional images and captions for them if you wish.
            The images required for these pages are here:
            LINK
        and the captions for the images are here (you should note that the name of the image precedes the caption):
            LINK

      5. Page 5: FORM I PAGE—This page will consist of the first form of two that will be required for the site, the submit idea form. It must utilize all of the same form fields as the form found in the current site:
            LINK

      6. Page 6: FORM II PAGE—This page will consist of the second form of two that will be required for the site, the volunteer form. It must utilize ALL of the same form fields as the form found here:
            LINK

       
    2. DESIGN REQUIREMENTS: 
      1. Color Scheme—you must use what is known as a Complimentary Color Scheme. A complimentary color scheme is a two-color scheme, but it is basically a scheme in which the large majority of the site is done monochromatically. Monochromatic means one color.
            This means that you start off with a site that follows a monochromatic scheme, which means that everything is to be some tint or shade of the same color. If you choose blue as your color, then that means absolutely everything in the site must be some variation of blue, including images. If you wish to show color images, then make the images clickable to link to larger, full-color versions of them.
            Once you establish this primary color, then you select its compliment to use as an accent color, for times when you need to focus attention or when you require greater contrast. Therefore, if blue is the main color for most of your site, then you would use some version of its compliment, orange, as your accent color (blue & orange, red & green, and yellow & purple).

      2. Page Layout—you must use CSS and/or tables to lay out all pages in a pleasing way. If you are not certain how to do this, consult our past classes and me for assistance.

    3. TECHNICAL REQUIREMENTS: 
      1. XHTML—ALL CODE for this site must be typed using STRICT XHTML standards. This means the following:
        • that all tags and their attributes must be typed in lower case;
        • that all attribute values must be typed in quotes and with units;
        • that the DTD for strict XHTML must be typed at the top of the document;
        • that all empty tags must be typed with the slash at the end, such as with the <br/> tag;
        • that none of the deprecated inline tags are to be used, such as the <font>, <b>, <u>, or <i> tags;
        • and, in addition, that the <tbody> and the <thead> tags, as well as the height attribute for the <table> and <td> tags, are not permitted.

            If you are uncertain about something, you can consult the requirements at the w3schools site (LINK), or you may ask me. Furthermore, the use of DreamWeaver is permitted; however, please note that DreamWeaver does not create strict XHTML. Therefore, you will have to go through the code and edit it yourself to make certain it follows strict standards.

      2. CSS—ALL STYLING must be done using CSS. We have used it extensively in this class, and you should have learned it in the previous DMA110 class; and further, as it is the standard in web-design, it is a requirement that this site use CSS for the styling of the page. All three levels of styles may be, and are encouraged to be used. Please consult me if you need additional assistance with this.
      3. JavaScript—As this is the heart and primary topic of this course, the presence and functionality of your JavaScript code is your primary task. More than 50% of the grade for this project will be based on this, so please focus on the scripts above all. Although XHTML, CSS, the content, and design of the site are all important, this is a JavaScript class after all, so keep in mind that an ugly site with functioning scripts, will do much better than a beautiful site with non-functioning scripts. There will be several scripts that you must include in your site:
        1. Form-validation (covered weeks 9-11)—The most important scripts will be the ones that deal with form validation. These must accomplish a few goals:
          1. They must first and foremost determine whether the form fields have been filled in;
          2. they must next determine whether text has been typed where it should be and that numerals are typed where they should be;
          3. they must prevent the data from being sent to output (such as in a document.write()) if the fields are not completely filled;
          4. and lastly, there must be a document.write() that prints out all the data that was typed into the form.
        2. image-replacement (covered week 12)—In two places, on the opening page and then somewhere else in your site—either as menu items or on the gallery page—you must use scripts to create mouse-over buttons with images. These are scripts that replace an image with another image when the mouse passes over the top of it, and then return the original image when the mouse moves away.
        3. PopUp Windows (covered week 12)—On the gallery page, the thumbnail images must be clickable and they must call a script that creates a new browser window with a larger full-color version of the images. The new browser windows must not have any menus at the top or scrollbars on the side or bottom. Accompanying each larger image, must be information about the image, the title, the date, and the media used, as well as a link that closes the window (closing the window can also be accomplished with JavaScript).
        4. Date insertion (covered week 13)—At the top of each of the content pages, you must insert the current date using JavaScript.

  4. FORM VALIDATION I: 
    1. Creating the Table—For organizing and laying-out our form, we are going to start with a table similar to one we have used in the past:
          LINK (week 6)
      Since we have gone over the process of creating a table step-by-step several times already, I will just show the desired table and its code below:
      information
       
       first name    
       
       last name    
       
       city    
       
       state    
       
       zip code    
       
           


        <table width="100%" border="1px" cellspacing="0px" cellpadding="0px">
          <tr>
            <td colspan="5" align="center">information</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td width="50px">&nbsp;</td>
            <td width="50px" align="right">first name</td>
            <td width="10px">&nbsp;</td>
            <td>&nbsp;</td>
            <td width="100px">&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td align="right">last name</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td align="right">city</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td align="right">state</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td align="right">zip code</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>
        </table>
       
    2. Adding the Form—The following table insterts the form elements and form tags.
      information
       
       first name   
       
       last name   
       
       city   
       
       state   
       
       zip code   
       
          



      <form>
        <table width="100%" border="1px" cellspacing="0px" cellpadding="0px">
          <tr>
            <td colspan="5" align="center">information</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td width="50px">&nbsp;</td>
            <td width="50px" align="right">first name</td>
            <td width="10px">&nbsp;</td>
            <td><input type="text"/></td>
            <td width="100px">&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td align="right">last name</td>
            <td>&nbsp;</td>
            <td><input type="text"/></td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td align="right">city</td>
            <td>&nbsp;</td>
            <td><input type="text"/></td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td align="right">state</td>
            <td>&nbsp;</td>
            <td><input type="text"/></td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td align="right">zip code</td>
            <td>&nbsp;</td>
            <td><input type="text"/></td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>

          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td><input type="button" value="submit"/></td>
            <td>&nbsp;</td>
          </tr>

          <tr>
            <td colspan="5">&nbsp;;</td>
          </tr>
        </table>
      </form>
       
    3. Designing the Layer—In order to create a layer and add needed designing to it, the following CSS must be typed into the head of the document:
       <style type="text/css">
         <!--
            #layer1    {width:300px;
                        height:200px;
                        position:absolute;
                        top:100px;
                        left:100px;
                        z-index:1;
                        border-width:1px;
                        border-color:#aaaaaa;
                        border-style:solid;
                        background-color:#000000;}
          //-->
        </style>
       
    4. Adding the IDs—So that the styling from the CSS code above, the layer id must be applied to the< span class="tag"><div> div tags surrounding the form and table. While we're at it, we'll add a few other IDs that will assist us with our JavaScript later:
      <div id="layer1">
        <form id="myForm">
          <table width="100%" border="1px" cellspacing="0px" cellpadding="0px">
            <tr>
              <td colspan="5" align="center">information</td>
            </tr>

            <tr>
              <td colspan="5">&nbsp;;</td>
            </tr>

            <tr>
              <td width="50px">&nbsp;</td>
              <td width="50px" align="right" id="firstText">first name</td>
              <td width="10px">&nbsp;</td>
              <td><input type="text" id="first"/></td>
              <td width="100px" id="firstWarn">&nbsp;</td>
            </tr>

            <tr>
              <td colspan="5">&nbsp;;</td>
            </tr>

            <tr>
              <td>&nbsp;</td>
              <td align="right" id="lastText">last name</td>
              <td>&nbsp;</td>
              <td><input type="text" id="last"/></td>
              <td id="lastWarn">&nbsp;</td>
            </tr>

            <tr>
              <td colspan="5">&nbsp;;</td>
            </tr>

            <tr>
              <td>&nbsp;</td>
              <td align="right" id="cityText">city</td>
              <td>&nbsp;</td>
              <td><input type="text" id="city"/></td>
              <td id="cityWarn">&nbsp;</td>
            </tr>

            <tr>
              <td colspan="5">&nbsp;;</td>
            </tr>

            <tr>
              <td>&nbsp;</td>
              <td align="right" id="stateText">state</td>
              <td>&nbsp;</td>
              <td><input type="text" id="state"/></td>
              <td id="stateWarn">&nbsp;</td>
            </tr>

            <tr>
              <td colspan="5">&nbsp;;</td>
            </tr>

            <tr>
              <td>&nbsp;</td>
              <td align="right" id="zipText">zip code</td>
              <td>&nbsp;</td>
              <td><input type="text" id="zip"/></td>
              <td id="zipWarn">&nbsp;</td>
            </tr>

            <tr>
              <td colspan="5">&nbsp;;</td>
            </tr>

            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td><input type="button" value="submit" id="submit"/></td>
              <td>&nbsp;</td>
            </tr>

            <tr>
              <td colspan="5">&nbsp;;</td>
            </tr>
          </table>
        </form>
      </div>


      With the CSS styles added via the Layer1 ID, the table and layer appear as follows:
      information
       
       first name   
       
       last name   
       
       city   
       
       state   
       
       zip code   
       
          

       
    5. Altering Properties—Before we begin the actual scripting, in the head of the document between the script tags, we will first initialize our function as function validate(). We will also have to call this function with our submit button down at the bottom of the form by using an event handler like so: onclick="validate()".
          Regarding the function of this script, what we ultimately want to occur is for the script to determine if all of the form fields have been filled out. If they have not, the script should prevent the form from being submitted and indicate to the user how and where the form should be completed. To change the properties, we will do three things, change the color of the text label, change the background color of the form field, and add the text "required" beside the field that should be filled out. Although this in itself does not stop the form data from being submitted, we will start here and build from it.
          To alter the properties of an element, such as its color, size, or background color is much like extracting and manipulating the value of an element. In fact, an element's value IS one of its properties. Therefore, a text field has several properties, it size and background color, as well as its value. Moreover, accessing those other properties, other than the value, is done in the same way, using the
      document.getElementById() function:

      <script language="javascript" type="text/javascript">
        <!--
          function validate()
          {
            document.getElementById("firstText").style.color = "#cc0000";
          }
       //-->
      </script>

      Using this little script, by pressing on the submit button, we will cause the words first name to become red. By adding the following line to the script, we can cause the background color of the form field to change colors:
      document.getElementByID("first").style.backgroundColor = "#ffaaaa;"
      And furthermore, we can insert text into the space to the left of the form element to indicate that the form field is required with these next lines:

      document.getElementById("firstWarn").innerHTML= "required";
      document.getElementById("firstWarn").style.color="#cc0000";
      document.getElementById("firstWarn").style.fontSize="10pt";

      In order to change the properties of all of the text fields, we should type the script as follows:


      <script language="javascript" type="text/javascript">
        <!--
          function validate()
          {
            document.getElementById("firstText").style.color = "#cc0000";
            document.getElementByID("first").style.backgroundColor = "#ffaaaa;"
            document.getElementById("firstWarn").innerHTML = "required";
            document.getElementById("firstWarn").style.color = "#cc0000";
            document.getElementById("firstWarn").style.fontSize = "10pt";


            document.getElementById("lastText").style.color = "#cc0000";
            document.getElementByID("last").style.backgroundColor = "#ffaaaa;"
            document.getElementById("lastWarn").innerHTML = "required";
            document.getElementById("lastWarn").style.color = "#cc0000";
            document.getElementById("lastWarn").style.fontSize = "10pt";


            document.getElementById("cityText").style.color = "#cc0000";
            document.getElementByID("city").style.backgroundColor = "#ffaaaa;"
            document.getElementById("cityWarn").innerHTML = "required";
            document.getElementById("cityWarn").style.color = "#cc0000";
            document.getElementById("cityWarn").style.fontSize = "10pt";


            document.getElementById("stateText").style.color = "#cc0000";
            document.getElementByID("state").style.backgroundColor = "#ffaaaa;"
            document.getElementById("stateWarn").innerHTML = "required";
            document.getElementById("stateWarn").style.color = "#cc0000";
            document.getElementById("stateWarn").style.fontSize = "10pt";


            document.getElementById("zipText").style.color = "#cc0000";
            document.getElementByID("zip").style.backgroundColor = "#ffaaaa;"
            document.getElementById("zipWarn").innerHTML = "required";
            document.getElementById("zipWarn").style.color = "#cc0000";
            document.getElementById("zipWarn").style.fontSize = "10pt";
          }
       //-->
      </script>

      Click on the submit button above to see the function at work.
       
    6. Adding a Conditional—Now, what we want to happen is not really simply to make text appear and make things change colors. As wonderful as that is, on its own it does little to help the user to fill out the form correctly. In fact, we DO NOT want it to happen if the user does in fact fill out the form properly. Therefore, it should only change colors, for example, when the user leaves the fields blank. In plain english then, our conditional would read as follows:

      IF (the form field is left blank)
      {
        then change the colors of the text
        and change the background color
        and add the word "required"
      }
      ELSE
      {
        DO NOT change the colors of the text
        DO NOT change the background color
        DO NOT add the word "required"
      }

      Using JavaScript terminology, it would read as follows:


      if (document.getElementById("first").value == "")
      {
        document.getElementById("firstText").style.color = "#cc0000";
        document.getElementById("first").style.backgroundColor = "#ffaaaa;"
        document.getElementById("firstWarn").innerHTML = "required";
        document.getElementById("firstWarn").style.color = "#cc0000";
        document.getElementById("firstWarn").style.fontSize = "10pt";
      }
      else
      {
        document.getElementById("firstText;).style.color = "#aa99cc";
        document.getElementById("first").style.backgroundColor = "#ffffff;"
        documetn.getElementById("firstWarn").innerHTML = "";
      }

      The same sort of conditional statement must be used for each of the text fields:


      if (document.getElementById("first").value == "")
      {
        document.getElementById("firstText").style.color = "#cc0000";
        document.getElementByID("first").style.backgroundColor = "#ffaaaa;"
        document.getElementById("firstWarn").innerHTML = "required";
        document.getElementById("firstWarn").style.color = "#cc0000";
        document.getElementById("firstWarn").style.fontSize = "10pt";
      }
      else
      {
        document.getElementById("firstText;).style.color = "#aa99cc";
        document.getElementById("first").style.backgroundColor = "#ffffff;"
        documetn.getElementById("firstWarn").innerHTML = "";
      }

      if (document.getElementById("last").value == "")
      {
        document.getElementById("lastText").style.color = "#cc0000";
        document.getElementByID("last").style.backgroundColor = "#ffaaaa;"
        document.getElementById("lastWarn").innerHTML = "required";
        document.getElementById("lastWarn").style.color = "#cc0000";
        document.getElementById("lastWarn").style.fontSize = "10pt";
      }
      else
      {
        document.getElementById("lastText;).style.color = "#aa99cc";
        document.getElementById("last").style.backgroundColor = "#ffffff;"
        documetn.getElementById("lastWarn").innerHTML = "";
      }

      if (document.getElementById("city").value == "")
      {
        document.getElementById("cityText").style.color = "#cc0000";
        document.getElementByID("city").style.backgroundColor = "#ffaaaa;"
        document.getElementById("cityWarn").innerHTML = "required";
        document.getElementById("cityWarn").style.color = "#cc0000";
        document.getElementById("cityWarn").style.fontSize = "10pt";
      }
      else
      {
        document.getElementById("cityText;).style.color = "#aa99cc";
        document.getElementById("city").style.backgroundColor = "#ffffff;"
        documetn.getElementById("cityWarn").innerHTML = "";
      }

      if (document.getElementById("state").value == "")
      {
        document.getElementById("stateText").style.color = "#cc0000";
        document.getElementByID("state").style.backgroundColor = "#ffaaaa;"
        document.getElementById("stateWarn").innerHTML = "required";
        document.getElementById("stateWarn").style.color = "#cc0000";
        document.getElementById("stateWarn").style.fontSize = "10pt";
      }
      else
      {
        document.getElementById("stateText;).style.color = "#aa99cc";
        document.getElementById("state").style.backgroundColor = "#ffffff;"
        documetn.getElementById("stateWarn").innerHTML = "";
      }

      if (document.getElementById("zip").value == "")
      {
        document.getElementById("zipText").style.color = "#cc0000";
        document.getElementByID("zip").style.backgroundColor = "#ffaaaa;"
        document.getElementById("zipWarn").innerHTML = "required";
        document.getElementById("zipWarn").style.color = "#cc0000";
        document.getElementById("zipWarn").style.fontSize = "10pt";
      }
      else
      {
        document.getElementById("zipText;).style.color = "#aa99cc";
        document.getElementById("zip").style.backgroundColor = "#ffffff;"
        documetn.getElementById("zipWarn").innerHTML = "";
      }

      information
       
       first name   
       
       last name   
       
       city   
       
       state   
       
       zip code   
       
          




Thursday, November 09, 2006

week 8: 11/03/06

Hi everybody,
    Today is Thursday, November 9, just one day before the mid-term project is due. As you should know, it consists of two parts: the multiplication table and the questions/answers. Although I do believe they are rather difficult problems to solve at first, I think once you understand what exactly is going on in the scripts, particularly the multiplication table, you'll find it is not really so terribly difficult. Somewhat tedious, perhaps, but not all that difficult. Basically what it amounts to is a simple for loop and some complicated concatenation.
    The second script is perhaps even a little easier, although it does include the new topic of JavaScript Arrays. But really, an array is really just a fancy variable, so that concept shouldn't really be too difficult either. This script too, like the first one, does contain some rather complicated concatenation and a simple for loop. I hope they aren't too taxing. But whatever the case, once you get these two things down, I think it's basically almost all downhill from here for the rest of the semester. The remainder of what we do this semester is just a series of repititions of some of the same things that we've already done, with some slightly different applications. There's really only one more difficult thing left for us this semester, and it doesn't come for another few weeks, rollover images and generating new browser windows. We'll work on this together in the development of an image gallery.
    This week's entry discusses last Friday's class, the introduction of Arrays and their usage in for loops, both of which are necessary for part two of the mid-term project. Carter-

  1. TOPICS:
    • LINK   Review: variables;
    • LINK   Declaring an Array
    • LINK   Populating an Array;
    • LINK   Accessing Array data;
    • LINK   Arrays and For Loops;
    • LINK   Creating Parallel Arrays.

  2. HOMEWORKMid-Term Project, Part II: using examples from the class discussion of arrays and of for loops, and by adding two additional buttons to the form of the first part of the mid-term, print out the questions and then the answers in the same blank layer as used in Part I.
     
  3. REVIEW: Variables—A variable is a named element in JavaScript that serves as a container for a single piece of data. If you can recall, to declare a new variable involves two (2) things, the var keyword, and a unique name, such as the example below:
        var myVariable;
    Once this is done, the new variable exists for use in the script; however, in order for it to serve its purpose, it must be given a piece of data to store. The data stored inside a variable is called its value. Providing a value to a variable for the first time in the script is known as initializing the variable, and it is done as demonstrated below:
        myVariable = 100;
    In this example it was given a numerical value, but it may also contain a string:
        myVariable = "My name is Carter";
    Although this string consists of a number of characters, 17 to be exact including spaces, it nonetheless counts as only one piece of data. Even if a string consists of many characters, they are unified by a pair of quotation marks, then the entire string is stored by the variable.
        These two steps, declaring a variable, and initializing a variable, may be combined into one step as the example below demonstrates:

        var myVariable ="My name is Carter";
        Accessing the data stored by a variable is as simple as calling the name of the variable in some sort of output function, such as an alert() function or a document.write() as shown in the example below:
        alert(myVariable);
     
  4. INTRODUCE: Arrays—JavaScript Arrays are in fact modified variables. They are created and used in almost exactly the same way, and, moreover, they serve the same function, to store data.
    1. Declaring Arrays—To declare an array involves the same terminology as declaring a variable in that the var keyword must be used, along with a unique name; however, in order to convert a normal variable into an array, the variable must be filled with what is known as the array object. This is done in the following way:
          var myFirstArray = new Array();
      Here you can see that instead of initializing the array by giving it some data to hold, it is initialized by creating a new array and placing it inside the variable. This converts it from a simple variable that can hold a single element of data into an object which can contain much more data than that. As has been stated, an array has the same function as a variable, to store data, but it can do so on a greatly expanded scale. Instead of storing only one piece of data, an array may store a relatively unlimited amount of data, an unlimited number of data elements. 
    2. Populating Arrays—Although declaring an array might be very similar to declaring a variable, the way that an array is given data to contain is somewhat different. Simply given the fact that it can contain more than one value, the name of the array will naturally refer to many data elements rather than just one value, so accessing the information held within the array must be handled differently. Likewise, putting the data inside the array, the process by which we add one piece of data after another to an array, and which is known as populating the array, must also be handled differently. As mentioned, the unique name of the array will refer to all the data stored inside of it; but each element of data must be given an index number in order to differentiate them from each other, data element number 1, data element number 2, data element number 3, etc. This is completed in a the special way seen below:
          var firstNames = new Array();
              firstNames[0] = "Julia";
              firstNames[1] = "Kim";
              firstNames[2] = "Carrie";
              firstNames[3] = "Guy";
              firstNames[4] = "Joel";
              firstNames[5] = "Carter";
      The first thing you should notice is that a number between square brackets refers to the value instead of simply the name of the array. Second, you should also notice that the numbers begin counting at zero (0) instead of one. This is a convention in most computer languages which count the number zero as an actual numerical value instead of the absence or lack of value. Zero is still a numerical value, albeit a 'zero value'. For this reason, it is considered the first value when counting up numerically, and it cannot be skipped. Third, our array ends at the number 5 above, which means that there are six data elements stored in the array. These numbers that refer to a data element in an array are known as index numbers, and one good thing to remember is that the last index number of an array is always one less than the actual number of elements in the array.
          An alternative way to populate an array is one which does away with the index in the initialization process, where the array is populated. This way can be more efficient, especially if the array has a relative few elements as it puts everything in a single line of code:

          var firstNames = new Array("Julia", "Kim", "Carrie", "Guy", "Joel", "Carter");
      Notice that it does away with the repetitive task of typing the name of the array over and over again as well as typing the index numbers. Instead, all of the data is typed between the parentheses following the array declaration. Nonetheless, simply because the index numbers are not typed, does not mean they are not needed. "Julia" still has the index number of zero (0), "Kim" has the index number of one (1), and so on, even if they are not typed in the declaration.
          For longer arrays, however, it is still recommended to type out the array names with their index numbers when populating the array as it is more explicit and specific that way. You can actually see which index number that corresponds to its data right beside it.
       
    3. Accessing Arrays—Now that we have figured out how to get data into an array (input), we must figure out how to access the information held within the array so that, for instance, we may have control over output or otherwise manipulate that data. As mentioned, an array is basically a modified variable. Therefore, since access of the data held within a variable simply entails calling the variable by its unique name, we can expect that arrays will behave similarly. Below is an example of code that would declare and populate an array, and then afterwards, call on the array for output with an alert function:
          var firstNames = new Array("Julia", "Kim", "Carrie", "Guy", "Joel", "Carter");
          alert(firstNames);
      This code produces the following results: LINK

          If you clicked on the link above and read the contents of the alert box, you hopefully noticed that all the names in the array were displayed, separated only by commas. This makes sense if you understand what we did, we called the array by name by placing it within the alert function. Doing the same thing with a variable would cause the variable to reveal its contents, the data held within. An array does likewise, reveal, the entirety of its contents, all data elements held within.
          The tricky part comes when you want to call on a single element of data out of the whole of the array. For example, what if I wanted to access the third data element of the array above, "Carrie". How might I do so? The thing to keep in mind here is not the name of the array, but instead the individualized index numbers corresponding to each data element. Now, since I want the third data element, and, since I know that the index number is always one less (since counting begins at zero instead of one), then that means that the it has the index number of 2. As a result, to access that particular data element the statements must read as follow:

          var firstNames = new Array("Julia", "Kim", "Carrie", "Guy", "Joel", "Carter");
          alert(firstNames[2]);
      This time, the alert will read a single name: LINK 
    4. Accessing Arrays with For Loops—In the previous examples, it is clear how to access either all the data elements of an array, or only one of the data elements of an array. What is not clear is how to access each one of the elements individually, or a series of them. As it is, if we wanted to display all the elements of the array, whether we use an alert() or a document.write(), we would not be able to style them in the way we wanted, or space them out in a particular way. We would only see each element of data listed horizontally and separated with commas, and there would be very little we could do to change it. However, if we access each element, one at a time as a part of a series we can begin to see a way out of this. For example:
      // below is the input section of the script
          var first = new Array();
              first[0] = "Julia";
              first[1] = "Kim";
              first[2] = "Carrie";
              first[3] = "Guy";
              first[4] = "Joel";
              first[5] = "Carter";

      // below is the output section of the script
          document.write(first[0]);
          document.write(first[1]);
          document.write(first[2]);
          document.write(first[3]);
          document.write(first[4]);
          document.write(first[5]);

      The first section is familiar. This is where we populate the array. The second section should also be familiar: here is where we access the data within the array, but instead of doing so with an alert() function, we produce the data in a series of document.write() functions. This is a very simple process by which we get a list of names, but it can be simplified to a single document write by way of concatenation:
          document.write(first[0] + first[1] + first[2] + first[3] + first[4] + first[5]);
      See the results: LINK

      Hopefully, you can see the problem here: most obviously, all the names are run together. There must be spaces concatenated in to the document write like so:

          document.write(first[0] +", "+ first[1] +", "+ first[2] +", "+ first[3] +", "+ first[4] +", and "+ first[5]);
      This will create a horizontal list of names separated by spaces and commas as they should be in order to be grammatically correct. To take it one step further to create a vertical list, instead of the spaces and commas, we should add in a bit of HTML:
          document.write(first[0] +"<br/>"+ first[1] +"<br/>"+ first[2] +"<br/>"+ first[3] +"<br/>"+ first[4] +"<br/>"+ first[5]);
      See the results: LINK

      But by taking these few steps, you might also notice the repetition in the statement. Many things are repeated more than once. In fact, everything is repeated in the statement more than once; that is, except the index numbers. The more you have to type, the more repetition there is, the more text and code you have to put into a script, the more opportunities for errors creep in. Therefore, it's always a good thing to seek out opportunites to make your code more efficient, to reduce the amount of repetition and thereby the amount of typing required. This is where the usefulness of looping statements we have learned about comes in to the picture. They allow us to type a statement or part of a statement one time, and then to loop through it as many times as we need. The document above is a perfect example, so let's narrow down the document.write() statement to its essential part, the part that is repeated over and over. That would be what is below:

          first[index number] +"<br/>quot;
      If you compare those three things; the name of the array first, the index number, and the <br/> tag, to the longer full document.write() function above, you see that it is really just a repetition of those three things over and over. A for loop can repeat it for us:
          for(var i=0; i<6; i++)
          {
            document.write(first[index number] + "<br/>");
          }
      From this bit of code, we can see from the for loop, that it will loop 6 times, and that means that the document.write() will run through 6 times. This is great, that is exactly what we want to happen: we have 6 data elements in the array, and our array will run through six times, but how do we change the index number each time we loop through our loop? The first time through the loop, the index number must be 0, the second time, it must be 1, the third time, 2, and so on. How might we get that to occur? Well, we need look no further than the for statement itself. The counter variable i changes from 0 to 6 during the course of the loop, just as the index number should. Why don't we then use the counter variable in place of the index number like so:
          for(var i=0; i<6; i++)
          {
            document.write(first[i] + "<br/>");
          }
      See the results: LINK 
    5. Creating Parallel Arrays—Often it is necessary to create multiple arrays to store multiple categories of data, one for first names, a second for last names, a third for telephone numbers, etc. Now, while this in itself may be useful, what makes it so powerul is the possibility of corresponding index numbers. If the index numbers of the first names array, correspond to the index numbers of the last names array, and so on, then a single index number can refer to a whole series of arrays across several categories of information.
      // below is the first array
          var first = new Array();
              first[0] = "Julia";
              first[1] = "Kim";
              first[2] = "Carrie";
              first[3] = "Guy";
              first[4] = "Joel";
              first[5] = "Carter";

      // below is the second array
          var last = new Array();
              last[0] = "Hutchinson";
              last[1] = "O'Brien";
              last[2] = "George";
              last[3] = "Emmanuel";
              last[4] = "Almoradie";
              last[5] = "Johnson";
      As you can see above, my name in the first array, for example, has the index number of 5. The same index number of the second array corresponds to my last name. The number 5, therefore, may refer to corresponding information in multiple arrays. I could go on to create arrays for other categories of information such as telephone number, address, city, state, zip, etc.
          Accessing multiple parallel arrays such as this is done in the same way as single arrays. The only difference is that there are even more repeating elements. See the example below of a loop that outputs the data from the two arrays above:

          for(var i=0; i<6; i++)
          {
            document.write(first[i] +" "+ last[i] + "<br/>");
          }
      See the results: LINK

      Knowing all of this should help you to solve Part II of the mid-term. Part II entails that you will type the questions in the following handout as the first array and the answers to those questions as the second array. They are to be printed out in the same blank layer used in Part I, and they are to be called by two additional buttons added to the form used in Part I.

      Here are the mid-term questions: LINK

Thursday, November 02, 2006

week 7: 10/27/06

Hello all,
    Well, didn't catch up as fast as I thought I would this week. Mid-Terms got the best of me too. In any case, I know a lot of you are having a difficult time with the first part of the mid-term project, but keep plugging and trying to figure it out.
    The key is test your scripts, and then re-test and re-test and re-test. There really is no other way. First, it helps you to begin to understand how the script works, and then how to solve the problem at hand.
    Also, using alert() functions here and there to monitor your scripts and the data helps a lot too. You can always take them out when you're finished working on it.
    Anyway, I'm around if you want help. Carter-

  1. TOPICS:
    • LINK   Review: creating a simple html table;
    • LINK   Review: putting a form into a table;
    • LINK   Review: Creating a layer with CSS;
    • LINK   Review: getElementById() function,
    • LINK   Review: event handlers,
    • LINK   Mid-Term: The script from the hand-out.

  2. HOMEWORKMid-Term Project, Part I: use the reading that I handed out in class, and the CSS & HTML file that we created in class, along with the script given on the last page of the handout to create a MULTIPLICATION TABLE. Everyone should start with the script given to you, but then you should modify it to contain not only the alert() function, but also a function which prints the table out into the 2nd layer.
     
  3. Review:
    1. Tables—See below the creation of a very simple table with five (5) rows and five (5) cells in each row:

        <table border="0px" width="100%">
      1  <tr>
            <td colspan="5">&nbsp;</td>
          </tr>
      2   <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
      3   <tr>
            <td colspan="5">&nbsp;</td>
          </tr>
      4   <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
      5   <tr>
            <td colspan="5">&nbsp;</td>
          </tr>
        </table>
      This code creates the table that you see below:

       
           
       
           
       

      Next, here are a couple of tables with the width of a few cells changed, made narrower, plus with the addition of the form elements. The code is place below the tables:

       
           
       
           
       

       
    2. Forms—In the next table, the form has been added to it. Below, you will find the code to create this table:

       
       type a number  
       
          
       

      <form name="myForm" id="myForm"/>
        <table border="0px" width="100%">
      1  <tr>
            <td colspan="5">&nbsp;</td>
          </tr>
      2   <tr>
            <td width="10px">&nbsp;</td>
            <td align="right">type a number</td>
            <td width="200px">
             <input type="text" id="userNum"/>
           </td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
      3   <tr>
            <td colspan="5">&nbsp;</td>
          </tr>
      4   <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>
             <input type="button" id="submit" value="submit">
           </td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
      5   <tr>
            <td colspan="5">&nbsp;</td>
          </tr>
        </table>
      </form>

      When the borders are turned off, the table should look like this:

       
       type a number  
       
          
       
       
    3. CSS Layer—In the next table, the form has been added to it. Below, you will find the code to create this table:Next, the div tags are added before the first </form> tag and after the last one, along with a CSS #ID to style and create a layer:

        <style type="text/css">
         <!--
          #layer1  {width:250px;
                    height:100px;
                    top:10px;
                    left:100px;
                    position:absolute;
                    z-index:1;
                    border-width:3px;
                    border-style:double;
                    border-color:#ccaaff;
                    font-family:Arial;
                    background-color:#110022;}
         //-->
        </style>

      The first <div> tag must also have the same ID as indicated in the CSS above, like so:
         <div id="layer1">
      This will change the simple table and form into a layer that may be positioned anywhere on the table. In addition, the ID will make the entire layer accessible to manipulation by our JavaScript.

       
       type a number  
       
          
       

      Below, another CSS ID is added; however, in order for this to show up in the page, another pair of <div> must be added with the second ID, shown below applied

        <style type="text/css">
         <!--
          #layer1  {width:250px;
                    height:100px;
                    top:10px;
                    left:100px;
                    position:absolute;
                    z-index:1;
                    border-width:3px;
                    border-style:double;
                    border-color:#ccaaff;
                    font-family:Arial;
                    background-color:#110022;}

          #layer2  {width:250px;
                    height:100px;
                    top:120px;
                    left:100px;
                    position:absolute;
                    z-index:2;
                    border-width:3px;
                    border-style:double;
                    border-color:#ccaaff;
                    font-family:Arial;
                    background-color:#110022;}

         //-->
        </style>

      And then down below in the HTML of the document, together with the ID:

          <div id="layer2">&nbsp;</div>
      With this new ID, then, applied to a second pair of <div> tags, a new layer is created, even though there is nothing but a blank space between the two tags of the second layer:

       
       type a number  
       
          
       

       
       
    4. document.getElementById()—If you recall from prior weeks, this function takes the action of searching through the entire document for a particular ID. Recall that ID's are assigned in a tag, and they may also be used by CSS to create layers, as we have done. We have also given IDs to our form elements. IDs are used by JavaScript to locate an item, this is why each ID must be unique. No two IDs can have the same name. JavaScript, once an ID is located, can manipulate the element in many ways.
          If a form element has an ID, then JavaScript can use the ID to extract data from it by calling on its value. The value of a text box is whatever the user types in the box, and the way JavaScript extracts it is as follows:

          document.getElementById("userNum").value
      This refers to whatever the user types in the box. If the user types nothing in the box, then the value is nothing. Once the data in the text box is extracted, however, in order for it to be used or manipulated, it must be stored in a variable. This is done like so:
          var multiple = document.getElementById("userNum").value
      Now, whatever the user types in the box will immediately be transferred and stored into the variable multiple, and, as you know, many things can be done with variables and the data stored inside of them.
       
    5. Event Handlers—As mentioned in prior classes, an event here refers to computer events, such as loading a web-page, or closing a web-page, or clicking on a button or link, or tapping the enter key. All of these are events among many others defined by JavaScript.
          Now, JavaScript can use these defined events to cause yet other events to occur. For example, as mentioned, the clicking of a mouse is an event and this event could cause another event, like the appearance of an alert box over the browser window. It may do so with the event handler known as onclick:
      onclick = "alert('hello')";
          This is also the event handler that we will use for the submit button in our form, but instead of calling an alert() function, we will call our own user-defined function, which we will name myTable():
          <input type="button" id="submit" value="submit" onclick="myTable()"/>
       
    6. Mid-Term Script—Once you get the HTML and CSS typed in your document and you have two layers similar to the ones shown above, and once you get the event handler typed into the button form element, it is time to type the script into the document. Since our event handler calls the myTable() function, then the name of our function in our script should be the same. It is as follows:

      <script language="javascript" type="text/javascript">
       <!- -

      1.   function myTable()
      2.   {
      3.     var multTable = prompt("Please enter a
      4.       number to create a multiplication
      5.       table","");
      6.     var theTable = "";
      7.     for(var i=1; i<=12; i++)
      8.     {
      9.       theTable += i + "x" +multTable+ "=" +(i
      10.        * multTable)+ "\n";
      11.     };
      12.     alert(theTable);
      13.    }

       //- - >
      </script>

      Now, let us go over this script line-by-line so that we can determine what is happening.

        Line 1:
        function myTable()
        Here is the simple function declaration including:
        1. the function keyword;
        2. the unique name of the function: myTable;
        3. the opening and closing parentheses; and
        4. the opening, line 2, and closing line 13, curly brackets for the entire function.

        Lines 3-5:
        var multTable = prompt("Please enter a number to generate a multiplication table","");
        The first variable declaration and its initialization including:
        1. the var keyword;
        2. the unique name of the variable: multTable;
        3. the assignment operator; and
        4. the prompt() function which requests data from the user.

        Line 6:
        var theTable = "";
        The second variable declaration and its initialization including:
        1. the var keyword;
        2. the unique name of the variable: theTable;
        3. the assignment operator; and
        4. the value assigned, which is an empty string. Without this, the value of the variable would be undefined or null. Try removing the = "" from this statement to see how it affects the entire function when run.

        Line 7:
        for(var i=1; i<=12; i++)
        The initialization of the for loop including:
        1. the for keyword;
        2. the initial value of the counter variable, i = 1: theTable;
        3. the test condition, i <= 12;
        4. the incrementation of the counter variable, i++; and
        5. the opening, line 8 and closing line 11 curly brackets of the for loop.

        Line 9:
        theTable += i + "x" +multTable+ "=" +(i * multTable)+ "\n";
        This line assigns the variable, theTable, a new value, and by doing so, what we have is the heart of this script. It creates the multiplication table one line at at time in the following way:
        1. assigns the variable, theTable, with a new value with the assignment operator +=. This operator retains the existing value and adds it to a new value, so the result is a combination of the previous value AND the new value.
        2. next, begins the concatentation of a line of the multiplication table with the value of i. The first time through the loop i = 1. The second time, i = 2, the third, i = 3, and so on up to 12.
        3. the second term of the concatenation is just a very short string, the character "X", which will represent the 'times' sign in the multiplication table;
        4. the third term of the concatenation is the value of the variable multTable, which is simply whatever the number the user typed in at the prompt function.
        5. the fourth term of the concatenation is another very short string, the character "=", which will represent the 'equal' sign in the multiplication table;
        6. the fifth term of the concatenation is a brief operation in which two variables are multiplied, i * multTable, the counter variable multiplied by the user's number. This is last number of each line of the table after the equal sign.
        7. the sixth term of the concatenation is the final short string, the escape character "\n", which is really just a hard return for the alert window, the equivalent of the <br/> for HTML.
        8. the result of the concatenation of these six terms will be a single line of the multiplication table. If it is the first line, then the result will be this string:
          "1 X 10 = 10".

        Line 12:
        alert(theTable);
        This is the final output of the multiplication table, contained within the variable, theTable, in an alert() function.

      Once you get the script to work, you will be able to click on the submit button in the form to activate it, or call the function; however, as the script is now written, it completely bypasses the input field of the form. Instead, it provides a prompt()function.
          Therefore, just getting the script to function is only the very beginning of this problem: what you must do to complete this script for Part I of the mid-term project are three things:

      1. eliminate the prompt()function;
      2. activate the form text field as the source for data input instead of the prompt();
      3. print out the results of the script (produce the output) in the other blank layer.

      Below, you should find an example. Type a number in the form field, and then click submit:
       
       type a number