convert string to object reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JimMenees

    convert string to object reference

    I know there's an easy answer to this which is escaping me:
    --------------------------------------------------------------------------
    -----------------------
    I have clickable thumb images which pass the image info to a window open script
    to display a larger version of the image along with a description of the image.


    The image info comes from the image element; the description string is in a
    hidden form var with the same name as the image element.

    // example:
    <a... <image name='imagename 1' ... >/a>
    <input type='hidden' name='imagename 1' value='image descriptor1' .....

    Problem:
    inserting the image name argument into document.myform .imagename1.val ue
    to get the value of the hidden field yields a string, not the value of that
    form field.

    Doing this:
    var imdesc = "document.myfor m." + imagename1 + ".value"

    yields a string var called imdesc with a value of
    "document.myfor m.imagename1.va lue"

    But I don't want that string, I want its object reference value.

    There is something very basic here I'm overlooking... so the question is:
    How do I convert the string to have it literally interpreted as the value of
    the document.form + imagename1 argument ??

    Many many thanks in advance,

    Jim

  • kaeli

    #2
    Re: convert string to object reference

    In article <20030923093603 .01111.00000024 @mb-m23.aol.com>,
    jimmenees@aol.c omNoSpam enlightened us with...[color=blue]
    > var imdesc = "document.myfor m." + imagename1 + ".value"
    >[/color]

    var imdesc = document.myform[imagename1].value

    -------------------------------------------------
    ~kaeli~
    Hey, if you got it flaunt it! If you don't, stare
    at someone who does. Just don't lick the TV screen,
    it leaves streaks.


    -------------------------------------------------

    Comment

    • steve stevo

      #3
      Re: convert string to object reference

      try

      var imdesc = eval("document. myform." + imagename1 + ".value")


      "JimMenees" <jimmenees@aol. comNoSpam> wrote in message
      news:2003092309 3603.01111.0000 0024@mb-m23.aol.com...[color=blue]
      > I know there's an easy answer to this which is escaping me:
      > --------------------------------------------------------------------------
      > -----------------------
      > I have clickable thumb images which pass the image info to a window open[/color]
      script[color=blue]
      > to display a larger version of the image along with a description of the[/color]
      image.[color=blue]
      >
      >
      > The image info comes from the image element; the description string is in[/color]
      a[color=blue]
      > hidden form var with the same name as the image element.
      >
      > // example:
      > <a... <image name='imagename 1' ... >/a>
      > <input type='hidden' name='imagename 1' value='image descriptor1' .....
      >
      > Problem:
      > inserting the image name argument into document.myform .imagename1.val ue
      > to get the value of the hidden field yields a string, not the value of[/color]
      that[color=blue]
      > form field.
      >
      > Doing this:
      > var imdesc = "document.myfor m." + imagename1 + ".value"
      >
      > yields a string var called imdesc with a value of
      > "document.myfor m.imagename1.va lue"
      >
      > But I don't want that string, I want its object reference value.
      >
      > There is something very basic here I'm overlooking... so the question is:
      > How do I convert the string to have it literally interpreted as the value[/color]
      of[color=blue]
      > the document.form + imagename1 argument ??
      >
      > Many many thanks in advance,
      >
      > Jim
      >[/color]


      Comment

      • Douglas Crockford

        #4
        Re: convert string to object reference

        > try[color=blue]
        >
        > var imdesc = eval("document. myform." + imagename1 + ".value")[/color]

        No! Don't try that. That is extremely bad style. The correct way to write it is

        var imdesc = document.myform[imagename1].value;

        It is almost always incorrect to use the eval() function.



        Comment

        • steve stevo

          #5
          Re: convert string to object reference

          Please tell me why ?


          "Douglas Crockford" <nospam@laserli nk.net> wrote in message
          news:bkpla6$1ic $1@sun-news.laserlink. net...[color=blue][color=green]
          > > try
          > >
          > > var imdesc = eval("document. myform." + imagename1 + ".value")[/color]
          >
          > No! Don't try that. That is extremely bad style. The correct way to write[/color]
          it is[color=blue]
          >
          > var imdesc = document.myform[imagename1].value;
          >
          > It is almost always incorrect to use the eval() function.
          >
          > http://www.crockford.com/javascript/survey.html
          >[/color]


          Comment

          • Douglas Crockford

            #6
            Re: convert string to object reference

            > > > try[color=blue][color=green][color=darkred]
            > > > var imdesc = eval("document. myform." + imagename1 + ".value")[/color][/color][/color]
            [color=blue][color=green]
            > > No! Don't try that. That is extremely bad style.
            > > The correct way to write it is[/color][/color]
            [color=blue][color=green]
            > > var imdesc = document.myform[imagename1].value;[/color][/color]
            [color=blue][color=green]
            > > It is almost always incorrect to use the eval() function.[/color][/color]
            [color=blue][color=green]
            > > See http://www.crockford.com/javascript/survey.html[/color][/color]
            [color=blue]
            > Please tell me why ?[/color]

            That is a fair question. The faq (http://jibbering.com/faq/#FAQ4_40) recommends
            avoiding eval(), but doesn't say why. There are lots of good reasons to avoid
            it.

            First, it is a crutch. It allows weak programmers to remain weak programmers by
            avoiding learning about simple language features such as subscript notation.
            JavaScript is a complete programming language. It is always better to use the
            language's features correctly. It is always worse to synthesize features with
            eval().

            eval() is expensive. It invokes the JavaScript compiler every time it is called.

            It has portability problems. Some interpreters vary in the context that eval()
            operates in.

            It makes it harder to reason about the correctness of a program. Verification
            tools (http://www.crockford.com/javascript/lint.html) cannot validate the
            correctness of an eval() expression.

            Error detection is postponed from compile time to run time. This makes testing
            more difficult and increases the likelihood that your errors will be shown to
            users.

            eval() can fail if the expression is not syntactically correct. The security
            properties of eval() are horrible.

            There are lots of very bad JavaScript books out there that routinely use eval().
            If you have a bad one, chuck it in the trash right now and go out and get a good
            one.

            When you are reviewing scripts and libraries, routinely reject code that makes
            inapporpriate use of eval(). Almost all uses are inappropriate.

            Comment

            Working...