eval

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

    eval


    why won't the following work

    for(var i=0;i<pics;i++) {
    eval('img'+i) = new Image(wth,hgt)
    eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
    }

    basically I am trying to create a numer of placeholders with thier
    associated imgages!

    thanks in advance
    stuart


  • Douglas Crockford

    #2
    Re: eval

    > why won't the following work[color=blue]
    >
    > for(var i=0;i<pics;i++) {
    > eval('img'+i) = new Image(wth,hgt)
    > eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
    > }[/color]

    Where did you get this code?

    Comment

    • Stuart

      #3
      Re: eval


      Douglas Crockford <nospam@laserli nk.net> wrote in message
      news:55f44$3fa9 5a14$436563ac$1 5511@msgid.mega newsservers.com ...[color=blue][color=green]
      > > why won't the following work
      > >
      > > for(var i=0;i<pics;i++) {
      > > eval('img'+i) = new Image(wth,hgt)
      > > eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
      > > }[/color]
      >
      > Where did you get this code?[/color]

      It is part of a bigger script that I am writting, the problem that I am
      facing is that I cannot asign the src value to a constucted variable

      I need to create a variable amount of placeholders and cache images such as

      img1 = new Image(100,200)
      img1.src = 'someURL'
      img2 = new Image(100,200)
      img2.src = 'someURL'
      img3 = new Image(100,200)
      img3.src = 'someURL'

      I am trying to construct the placeholder through a loop as folows

      for(var i=1;i<pics;i++) {
      ( img + i ) = new Image(100,200)
      (img + i).src = 'someURL'
      }

      what I don't know is how to join img and i




      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: eval

        Stuart wrote:
        [color=blue]
        > why won't the following work[/color]

        Because it's evil[tm] eval(...). Besides, you are neither telling what you
        are consider working nor what `pics' aso. are. Nevertheless, despite its
        bad style, the below code does create Image objects and assigns a value to
        their `src' property, so I consider it working.
        [color=blue]
        > for(var i=0;i<pics;i++) {
        > eval('img'+i) = new Image(wth,hgt)
        > eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
        > }
        >
        > basically I am trying to create a numer of placeholders with thier
        > associated imgages![/color]

        Provided that `pics' stores the number of Image objects to create, that
        `wth' and `hgt' are values for the width and height of *each* object and
        that the image resources to be accessed are available via the URLs
        http://www.mypics/X1.gif, where X is a number from 0 to pics - 1, use

        var myImages = new Object();
        for (var i = 0; i < pics; i++)
        {
        myImages['img' + i] = new Image(wth, hgt);
        myImages['img' + i].src = 'http://www.mypics/' + i + '1.gif';
        }

        instead.


        PointedEars

        Comment

        • Douglas Crockford

          #5
          Re: eval

          > > > why won't the following work[color=blue][color=green][color=darkred]
          > > >
          > > > for(var i=0;i<pics;i++) {
          > > > eval('img'+i) = new Image(wth,hgt)
          > > > eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
          > > > }[/color]
          > >
          > > Where did you get this code?[/color]
          >
          > It is part of a bigger script that I am writting, the problem that I am
          > facing is that I cannot asign the src value to a constucted variable[/color]

          Who told you to use eval()?

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: eval

            Thomas 'PointedEars' Lahn wrote:
            [color=blue]
            > Stuart wrote:[color=green]
            >> why won't the following work[/color]
            >
            > Because it's evil[tm] eval(...). Besides, you are neither telling what you
            > are consider working nor what `pics' aso. are. Nevertheless, despite its
            > bad style, the below code does create Image objects and assigns a value to
            > their `src' property, so I consider it working.
            >[color=green]
            >> for(var i=0;i<pics;i++) {
            >> eval('img'+i) = new Image(wth,hgt)
            >> eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
            >> }[/color][/color]

            Hm, having tested it in Mozilla 1.5b and IE 6.0 SP-1, I'm getting a script
            error because eval(...) isn't allowed as left-hand side expression (IE says
            explicitely: "Cannot assign to a result of a function" (or similar,
            translated it from German). Maybe I'm mixing up something here, but I
            remember ECMAScript implementations where it was possible to use it there.
            However, it's still bad style.


            PointedEars

            Comment

            • Stuart

              #7
              Re: eval


              Douglas Crockford <nospam@laserli nk.net> wrote in message
              news:8a03$3fa95 f61$436563ac$15 733@msgid.megan ewsservers.com. ..[color=blue][color=green][color=darkred]
              > > > > why won't the following work
              > > > >
              > > > > for(var i=0;i<pics;i++) {
              > > > > eval('img'+i) = new Image(wth,hgt)
              > > > > eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
              > > > > }
              > > >
              > > > Where did you get this code?[/color]
              > >
              > > It is part of a bigger script that I am writting, the problem that I am
              > > facing is that I cannot asign the src value to a constucted variable[/color]
              >
              > Who told you to use eval()?
              >[/color]
              I am trying anything....inc luding asking you!
              Are you getting the jist of what I am trying to achieve?


              Comment

              • Stuart

                #8
                Re: eval


                Thomas 'PointedEars' Lahn <PointedEars@we b.de> wrote in message
                news:bobnnp$1cg hhm$1@ID-107532.news.uni-berlin.de...[color=blue]
                > Thomas 'PointedEars' Lahn wrote:
                >[color=green]
                > > Stuart wrote:[color=darkred]
                > >> why won't the following work[/color]
                > >
                > > Because it's evil[tm] eval(...). Besides, you are neither telling what[/color][/color]
                you[color=blue][color=green]
                > > are consider working nor what `pics' aso. are. Nevertheless, despite its
                > > bad style, the below code does create Image objects and assigns a value[/color][/color]
                to[color=blue][color=green]
                > > their `src' property, so I consider it working.
                > >[color=darkred]
                > >> for(var i=0;i<pics;i++) {
                > >> eval('img'+i) = new Image(wth,hgt)
                > >> eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
                > >> }[/color][/color]
                >
                > Hm, having tested it in Mozilla 1.5b and IE 6.0 SP-1, I'm getting a script
                > error because eval(...) isn't allowed as left-hand side expression (IE[/color]
                says[color=blue]
                > explicitely: "Cannot assign to a result of a function" (or similar,
                > translated it from German). Maybe I'm mixing up something here, but I
                > remember ECMAScript implementations where it was possible to use it there.
                > However, it's still bad style.
                >[/color]
                Yes, I am getting these exact same errors,. However, I am pleased you
                understood what I am trying to do, May be it is "bad style" but I am new to
                javaScript, so may be you could point me in the way of "Good Style" and the
                correct way of scripting.


                Comment

                • Lee

                  #9
                  Re: eval

                  Stuart said:[color=blue]
                  >
                  >
                  >Douglas Crockford <nospam@laserli nk.net> wrote in message
                  >news:55f44$3fa 95a14$436563ac$ 15511@msgid.meg anewsservers.co m...[color=green][color=darkred]
                  >> > why won't the following work
                  >> >
                  >> > for(var i=0;i<pics;i++) {
                  >> > eval('img'+i) = new Image(wth,hgt)
                  >> > eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
                  >> > }[/color]
                  >>
                  >> Where did you get this code?[/color]
                  >
                  >It is part of a bigger script that I am writting, the problem that I am
                  >facing is that I cannot asign the src value to a constucted variable
                  >
                  >I need to create a variable amount of placeholders and cache images such as
                  >
                  >img1 = new Image(100,200)
                  >img1.src = 'someURL'
                  >img2 = new Image(100,200)
                  >img2.src = 'someURL'
                  >img3 = new Image(100,200)
                  >img3.src = 'someURL'
                  >
                  >I am trying to construct the placeholder through a loop as folows
                  >
                  >for(var i=1;i<pics;i++) {
                  > ( img + i ) = new Image(100,200)
                  > (img + i).src = 'someURL'
                  >}
                  >
                  >what I don't know is how to join img and i[/color]

                  Whenever you find yourself using eval(), you've probably
                  overlooked a simpler way to do something.

                  var img=new Array();
                  for(var i=0;i<pics;i++) {
                  img[i] = new Image(100,200);
                  img[i].src = 'someURL';
                  }

                  Comment

                  • Douglas Crockford

                    #10
                    Re: eval

                    > > Who told you to use eval()?[color=blue][color=green]
                    > >[/color]
                    > I am trying anything....inc luding asking you!
                    > Are you getting the jist of what I am trying to achieve?[/color]

                    It is almost always extremely wrong to use eval(). Who is teaching you to
                    program so badly?

                    Comment

                    • Janwillem Borleffs

                      #11
                      Re: eval


                      "Stuart" <kffgtr@yytd.gf t> schreef in bericht
                      news:bobmgv$95i $1@newsg4.svr.p ol.co.uk...[color=blue]
                      >
                      > I am trying to construct the placeholder through a loop as folows
                      >
                      > for(var i=1;i<pics;i++) {
                      > ( img + i ) = new Image(100,200)
                      > (img + i).src = 'someURL'
                      > }
                      >
                      > what I don't know is how to join img and i
                      >[/color]

                      Try it as follows:

                      for(var i=1;i<pics;i++) {
                      window['img' + i] = new Image(100,200)
                      window['img' + i].src = 'someURL'
                      }


                      JW



                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: eval

                        Stuart wrote:

                        [Repaired quoting, please read

                        [color=blue]
                        > Thomas 'PointedEars' Lahn <PointedEars@we b.de> wrote [...][color=green]
                        >> Thomas 'PointedEars' Lahn wrote:
                        >> [...] May be it is "bad style" but I am new to
                        >> javaScript, so may be you could point me in the way of "Good Style" and the
                        >> correct way of scripting.[/color][/color]

                        I already did, what part of it did you not understand?

                        Maybe if you would shorten the quotes to what is required for following the
                        discussion, which saves disk space and bandwidth, and eases reading, you
                        were forced to read postings more thoroughly:




                        PointedEars

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: eval

                          Stuart wrote:
                          [color=blue]
                          > Thomas 'PointedEars' Lahn <PointedEars@we b.de> wrote [...][color=green]
                          > > However, [eval(...) is] still bad style.[/color]
                          >
                          > [...] May be it is "bad style" but I am new to javaScript, so may be you
                          > could point me in the way of "Good Style" and the correct way of
                          > scripting.[/color]

                          I already did, what part of it did you not understand?

                          Maybe if you would shorten the quotes to what is required for following the
                          discussion, which saves disk space and bandwidth, and eases reading, you
                          were forced to read postings more thoroughly:




                          HTH

                          PointedEars

                          Comment

                          • Stuart

                            #14
                            Re: eval

                            Many thanks Lee.....works perfectly

                            thanks again
                            Stuart


                            Lee <REM0VElbspamtr ap@cox.net> wrote in message
                            news:bobnfk0jkg @drn.newsguy.co m...[color=blue]
                            > Stuart said:[color=green]
                            > >
                            > >
                            > >Douglas Crockford <nospam@laserli nk.net> wrote in message
                            > >news:55f44$3fa 95a14$436563ac$ 15511@msgid.meg anewsservers.co m...[color=darkred]
                            > >> > why won't the following work
                            > >> >
                            > >> > for(var i=0;i<pics;i++) {
                            > >> > eval('img'+i) = new Image(wth,hgt)
                            > >> > eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
                            > >> > }
                            > >>
                            > >> Where did you get this code?[/color]
                            > >
                            > >It is part of a bigger script that I am writting, the problem that I am
                            > >facing is that I cannot asign the src value to a constucted variable
                            > >
                            > >I need to create a variable amount of placeholders and cache images such[/color][/color]
                            as[color=blue][color=green]
                            > >
                            > >img1 = new Image(100,200)
                            > >img1.src = 'someURL'
                            > >img2 = new Image(100,200)
                            > >img2.src = 'someURL'
                            > >img3 = new Image(100,200)
                            > >img3.src = 'someURL'
                            > >
                            > >I am trying to construct the placeholder through a loop as folows
                            > >
                            > >for(var i=1;i<pics;i++) {
                            > > ( img + i ) = new Image(100,200)
                            > > (img + i).src = 'someURL'
                            > >}
                            > >
                            > >what I don't know is how to join img and i[/color]
                            >
                            > Whenever you find yourself using eval(), you've probably
                            > overlooked a simpler way to do something.
                            >
                            > var img=new Array();
                            > for(var i=0;i<pics;i++) {
                            > img[i] = new Image(100,200);
                            > img[i].src = 'someURL';
                            > }
                            >[/color]


                            Comment

                            • Stuart

                              #15
                              Re: eval


                              Douglas Crockford <nospam@laserli nk.net> wrote in message
                              news:2a40e$3fa9 6685$436563ac$1 6169@msgid.mega newsservers.com ...[color=blue][color=green][color=darkred]
                              > > > Who told you to use eval()?
                              > > >[/color]
                              > > I am trying anything....inc luding asking you!
                              > > Are you getting the jist of what I am trying to achieve?[/color]
                              >
                              > It is almost always extremely wrong to use eval(). Who is teaching you to
                              > program so badly?
                              >[/color]

                              And your input has been ?????





                              Comment

                              Working...