SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member walkamongus's Avatar
    Join Date
    Jul 2007
    Location
    VA
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply Javascript: 3 Questions

    1) Chapter 3, Page 69:

    In the grey box at the bottom of the page, when performing the check to see if getElementById returns, a node object, do you really need the IF conditional statement? Couldn't you just use the alert function and just check the alert box to make sure it doesn't return "null"? The code as written seems like it wouldn't give you an alert if the value of target is null. Wouldn't you want an alert if the value is null?


    2) Chapter 3, Page 86:

    Grey box at the bottom of the page...
    var body = document.getElementsByTagName("body")[0];
    Why do we need the index on the end? Does this restrict our variable value to only the first <body> element in our HTML?


    3) Chapter 3, Page 97:

    Looking at the entire program StripeyTables...

    I tried to go through the entire program as if I were the computer running the code. I understand the first cycle through the program, but after the first cycle, all of our counter variable are incremented.

    This seems like it would cause a problem to me because if we have 2 identical tables, then after the first table is striped (the first cycle through the entire code), the variable "tbodys" would then contain a node list of the tbody elements of the second table, which would be the same as the first table.

    When the second for loop begins, our j counter variable would be equal to 1 after being incremented the first time and this would fail the
    "j < tbodys.length" condition because tbodys.length would have a value of 1 like it did on the first cycle. Thus, the loop would be exited.

    Does this code only stripe 1 table or should it stripe all tables on a page? Am I way off base?

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by walkamongus View Post
    1) Chapter 3, Page 69:

    In the grey box at the bottom of the page, when performing the check to see if getElementById returns, a node object, do you really need the IF conditional statement? Couldn't you just use the alert function and just check the alert box to make sure it doesn't return "null"? The code as written seems like it wouldn't give you an alert if the value of target is null. Wouldn't you want an alert if the value is null?
    If target is null and you attempt to alert(target.nodeName), you'll generate a JavaScript error along the lines of "target has no properties", as described on p.290.

    If we were simply doing alert(target), then yes we could get away without checking for the null value, but as soon as you try to access a property or method of an object, you need to make sure the object is not null first.

    2) Chapter 3, Page 86:

    Grey box at the bottom of the page...
    var body = document.getElementsByTagName("body")[0];
    Why do we need the index on the end? Does this restrict our variable value to only the first <body> element in our HTML?
    document.getElementsByTagName("body") gives you a node set containing all the body elements in the document (of which there will be only one, of course). The index selects the first element in that node set (the first and only body element).

    3) Chapter 3, Page 97:

    Looking at the entire program StripeyTables...

    I tried to go through the entire program as if I were the computer running the code. I understand the first cycle through the program, but after the first cycle, all of our counter variable are incremented.
    Correct, but each time the computer arrives at a for loop, it runs the first statement within the parentheses (e.g. var i = 0), which resets the counter variable.

    This seems like it would cause a problem to me because if we have 2 identical tables, then after the first table is striped (the first cycle through the entire code), the variable "tbodys" would then contain a node list of the tbody elements of the second table, which would be the same as the first table.
    Consider this line of the code:
    Code JavaScript:
    var tbodys = tables[i].getElementsByTagName("body")

    While it is processing the first table in the document, the variable i will be equal to 0, and this line will assign to tbodys a node list containing the tbody elements of the first table in the document.

    When it returns to this line to process the second table in the document, the variable i will have been incremented to 1, and this line will assign to tbodys a node list containing the tbody elements of the second table in the document. This overwrites the previous value of the variable (the node list for the first table).

    Whether or not the two tables are identical makes no difference at all.

    When the second for loop begins, our j counter variable would be equal to 1 after being incremented the first time and this would fail the
    "j < tbodys.length" condition because tbodys.length would have a value of 1 like it did on the first cycle. Thus, the loop would be exited.
    No, because the first thing that happens each time execution reaches that second for loop is that j is set to 0:

    Code JavaScript:
    for (var j = 0; j < tbodys.length; j++)

    See the var j = 0; statement inside the opening parenthesis of the for loop? That statement gets run each time the browser arrives at the start of the for loop, so the counter variable is reset each time this loop is run.

    Does this code only stripe 1 table or should it stripe all tables on a page? Am I way off base?
    The code will stripe as many tables as you want it to. Feel free to try it yourself!

    If you're still unclear on what's happening with the counter variables, I'd suggest skipping ahead to Chapter 7, which shows you how to use Firebug to step through the execution of a script line-by-line, while watching the values of variables change.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    SitePoint Member walkamongus's Avatar
    Join Date
    Jul 2007
    Location
    VA
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for helping me out. That clears up all my questions for that chapter. Your book is awesome so far, I just have to read each chapter 5 or 6 times because I'm starting with absolutely zero programming experience at all (my b.s. in communications is worthless so I'm teaching myself web design and development). I'm getting to where I can think like a computer but its slow going.

    Again, thanks for the help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •