eval() problem for dynamic object referencing

Discussion in 'Javascript' started by Simon, Oct 28, 2003.

  1. Simon

    Simon Guest

    Am using the following code.

    <script language="JavaScript1.2">

    function setquantity(productindex,productquantity)
    {
    //create the reference object
    irefname_none = eval("document." + productindex + "none");

    <snip>

    and set quantity is called as follows further on in the HTML

    <snip>

    <td><img style="filter:alpha(opacity=20);-moz-opacity:0.2"
    name="1_none" src="none.gif" onclick="setquantity('1','none')" /></td>

    <snip>

    when I run this, the javascript console shows me an error as follows

    Error Missing ; before statement
    Line 9
    document.1none

    Line 9 is the eval line. As a test, if I remove the productindex from
    the eval statement it works fine, so doesn't seem to like
    productindex, despite a string being passed.

    Any suggestions appreciated. Testing and developing this on Mozilla
    1.2.1, Linux

    Regards
    Simon
    Simon, Oct 28, 2003
    #1
    1. Advertising

  2. Simon

    Lee Guest

    Simon said:
    >
    >Am using the following code.
    >
    ><script language="JavaScript1.2">
    >
    >function setquantity(productindex,productquantity)
    >{
    > //create the reference object
    > irefname_none = eval("document." + productindex + "none");


    This is one reason why you should never use eval() for
    dynamic object referencing. Others include the fact
    that it is very inefficient.

    If you're referencing images, you could use:

    irefname_none=document.images[productindex+"none"];

    but the more general solution would be to use an ID
    instead of NAME attribute, and use:

    irefname_none=document.getElementById(productindex+"none");
    Lee, Oct 28, 2003
    #2
    1. Advertising

  3. (Simon) writes:

    Yes, using eval for dynamic object referencing *is* a problem, so
    don't do it! :)

    > Am using the following code.
    >
    > <script language="JavaScript1.2">


    In HTML 4, the type attribute is required. The following is the correct,
    sufficient and recommended script start tag:

    <script type="text/javascript">

    Are you aware of the differences between Javascript versions 1.2 and
    1.3 and which browsers change behavior (to the deprecated 1.2
    behavior) because of your language attribute?

    I know the differences, but not which browsers honors the version
    number in the language attribute and implements 1.2 behavior.
    You most likely don't want to use Javascript 1.2.

    > function setquantity(productindex,productquantity)
    > {
    > //create the reference object
    > irefname_none = eval("document." + productindex + "none");


    irefnam_none = document[productindex+"none"];

    No eval, no problem.
    Are you sure the object you are looking for is a property of the
    document object, and that it has a name like "1none"?

    Your problem is probably that you use the dot-notation with a property
    name that is not a legal identifier. I.e.,
    document.1none
    is illegal since "1none" is not a legal identifer name. You must use
    document["1none"]
    for that kind of property names.

    You probably mean to use
    irefname_none = document.getElementById(productindex+"_none"):
    instead.

    > and set quantity is called as follows further on in the HTML
    >
    > <snip>
    >
    > <td><img style="filter:alpha(opacity=20);-moz-opacity:0.2"
    > name="1_none" src="none.gif" onclick="setquantity('1','none')" /></td>


    This call to setquantity would make
    irefname_none = document["1none"]
    If you want to refer to this image itself, a better line would be

    irefname_none = document.images[productindex+"_none"];
    Or better yet, just send the element itself as an argument:
    onclick="setquantity(this,'none')"
    Then you have the image element readily available as the first argument,
    and you don't need to go through

    > when I run this, the javascript console shows me an error as follows
    >
    > Error Missing ; before statement
    > Line 9
    > document.1none


    Yes, a property called "1none" is not accessible using the dot-notation,
    you must use square brackets.

    > Any suggestions appreciated. Testing and developing this on Mozilla
    > 1.2.1, Linux


    Drop eval completely. Use names that start with a letter instead of a
    number. Use W3C DOM functions or collections to access elements
    (document.getElementById or document.images).

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Oct 28, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,360
    Brock Allen
    Apr 4, 2005
  2. Colin Hemmings

    dynamic object referencing

    Colin Hemmings, Jan 18, 2006, in forum: Java
    Replies:
    4
    Views:
    319
    isamura
    Jan 18, 2006
  3. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    524
    Karl Seguin [MVP]
    Jun 16, 2006
  4. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,410
    Bruno Desthuilliers
    Jan 8, 2009
  5. Liang Wang
    Replies:
    8
    Views:
    124
    Ben Morrow
    Feb 2, 2008
Loading...

Share This Page