SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast HappyPike's Avatar
    Join Date
    Jan 2005
    Location
    USA
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Are these equivalent?

    Hi,

    I am pretty new to Javascript and have a few questions.

    1) Are these two code snippets equivalent?

    Code:
    document.getElementById("project_table").rows.item(numrows).id = "row" + numrows;
    document.getElementById("project_table").rows.item(numrows).name = "row" + numrows; 
    funct_name = eval("funct_row" + numrows);
    row_name = eval("row"+numrows);
    and

    Code:
    document.getElementById("project_table").rows[numrows].id = "row" + numrows;
    document.getElementById("project_table").rows[numrows].name = "row" + numrows;
    funct_name = eval("funct_row" + numrows);
    row_name = eval("row"+numrows);
    If yes, is the second one more cross browser compatible?

    2) Could I use getElementById instead of eval for the last 2 lines in each one?

    Thanks.
    The only way to have a friend is to be one. ~ Ralph Waldo Emerson

  2. #2
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From a quick test, they do seem to be equivalent (at least in Safari), though I've not seen the .item(n) syntax used before. I would probably still use the [n] syntax, because then you can think of .rows as being just a normal array.

    Stylistically, I'd probably write the code like this:

    Code:
    var table = document.getElementById("project_table");
    
    var fun_name = "row_fun_" + row_index;
    var row_name = "row" + row_index;
    
    table.rows[row_index].id = row_name;
    table.rows[row_index].name = row_name;
    Note that I only evaluate .getElementById and row_name= once, and that I've changed the indexing variable from "numrows" to "row_index". "numrows" sounds to me like "the number of rows", but there is never an item at that index, because the row indicies go from 0..(the number of rows - 1).

    Also notice that you don't need the eval, and that when accessing variables for the first time, I've put var infront of them, to make sure that they exist in the correct scope.

    hth,
    Douglas
    Hello World

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    They are equivalent, and they are equally compatible. The .item() syntax is a result of standardisation, to make it easier for other languages than Javascript, to implement the DOM API. This form is rarely used in Javascript though, so just go with the second one, as everybody else does.

    eval evaluates (runs) the string given, as code. Consider the following:
    Code:
    var foo = 42;
    
    var result = foo;
    alert(result); // yields 42
    
    var result = eval("foo");
    alert(result); // yields 42
    It's generally considered bad practise to use eval, because it can be very hard to follow, and because it's inherently unsafe (Maybe not as much of an issue in Javascript, but still). It is very rare that you have a variable, where you only know its name at runtime, and thus need to use eval() to get at it. You can almost always solve this differently.

    In your case, matters are complicated a bit further. Javascript has a "shortcut" build in, which is supposed to make the programmers work easier by relieving him of thinking. Unfortunately, this can lead to some confusion. If there is an element in the HTML document (the DOM), which has an id or name attribute, there will automagically be a global variable in Javascript, which contains a reference to this HTML element. You could - and should - retrieve this element by using the DOM API. Eg. with document.getElementById("foo"), but because it is also available as a global variable with the same name, you can actually access it with eval("foo"). As mentioned, this is definitely not good practise though.

  4. #4
    SitePoint Enthusiast HappyPike's Avatar
    Join Date
    Jan 2005
    Location
    USA
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks for the replies!
    The only way to have a friend is to be one. ~ Ralph Waldo Emerson


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
  •