Tuesday, July 03, 2007

Summer 2007

week 8: 6/29

***Addendum***

hello all of you,
i'm very proud of you for turning in your mid-term's to me by today even though you didn't have class with me. i couldn't be happier. i haven't looked at them yet (it's still friday night), but just having them all here pleases me immensely.

you all have shown to me that you're interested in working hard, and to me, that's more important than you getting everything 100% correct. you should be proud of yourselves.

enjoy your weekend (you're probably completely done with mid-terms by now), and your week without DMA106. no javascript homework this week. see you all on friday.
carter-

***Addendum***

Hello all,
    I know a lot of you are having a difficult time with 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.
    If you do not understand the original script, there is no way you can do this mid-term. I went over it in class, and the reading that I handed out goes over it, so you should understand what is happening. If not, then please take the time to go through it and figure out how it works.
    One hint I can give to you is to build up the multiplication table together WITH the <table> </table> tags and then place it all in your variable.
    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. See you Friday. 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.
    • LINK   Mid-Term: How to create an HTML Table with JavaScript.

  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  
       
          
       


       
    7. JavaScript & HTML Tables—In many ways, this next script is very similar to the above script where we create the multiplication tables (not to mention the fact that you'll need both to complete your mid-term project properly). The script above constructs a multiplication table line-by-line, and this is what we do when we type an HTML table: we create it row-by-row. Conceptually, the process is identical: it is like creating a house made of bricks, in which you lay one course (one row) of bricks down and then add another on top, and another and another, one row at a time until the entire wall is complete. Any table is created this way, one row at a time.
          However, since we are using JavaScript, and since we will be using the looping structure of a
      for loop, we have to determine what elements of our HTML table must be repeated: you must recall that looping structures in JavaScript are used for repetitive tasks. Since the construction of a table is in fact a repetitive task (it uses multiple rows), the looping structure of a for loop is ideal.
          Okay then, so what is repeated in an HTML table? Why the rows and the tags that create them of course. The
      <table> tags are not repeated as there is only one at the start of the table, and one at the end of the table; but what is repeated is the series of <tr> and <td> tags in between them. As a result, we know that that series of tags is what goes into the loop.

      See the script below:


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

      1.   function htmlTable()
      2.   {
      3.     var rowNum = prompt("Please enter a
      4.       number of rows for an HTML
      5.       table","");
      6.     var theTable = "<table border='1px'
      7.       width='500px'>";
      8.     for(var i=1; i<=rowNum; i++)
      9.     {
      10.        theTable += "<tr><td>&nbsp;
      11.          </td></tr>";
      12.     };
      13.     theTable += "</table>";
      14.     document.write(theTable);
      15.   };


       / /- ->
      </script>


      The important thing to notice in the script above is what is within the loop in lines 10 and 11, and that is the string that creates the <tr> and <td> tags.

0 Comments:

Post a Comment

<< Home