0

I'm wondering whether I can assign the pointer of a variable as something along the lines of

"image"+i+".src"

I've tried using eval, because that's the only suggestion I've found, but I'm being thrown a undefined variable error. Here's my code, if someone wouldn't mind taking a look:

<html>
    <head>
        <script type="text/javascript">
        <!--
            m = 0
            x = 0
            image=[  //Initializes the Array for Image URLs, Add object by adding the full URL in "" with , in between each entry
                "http://i.imgur.com/OaElB10.jpg",
                "http://i.imgur.com/NTYiEB9.jpg",
                "http://i.imgur.com/X1jreGc.jpg"]
            function ImgPreloadHandler()
            {                
                l = image.length
                for(t=0; t<l; t++)
                {
                    var image+t = new image()
                        image+t = image[t]
                }
            }
            ImgPreloadHandler()
        //-->
        </script>
    </head>
    <body>
    <img src="http://i.imgur.com/mv3sV8m.png" name="slide" width="796" height="600" />
        <script>
            function slideit()
            {            
                var step = 0
                var z = step
                if (!document.images)//if browser does not support the image object, exit.
                    return
                        document.images.slide.src=
                    if (step<x)
                        step++
                    else
                        step=1
                setTimeout("slideit()",2500)//call function "slideit()" every 2.5 seconds
            }
        slideit()
        </script>
    </body>
</html>
5
  • 1
    If you want to generate a list of values, use an array. Commented Dec 11, 2013 at 18:49
  • possible duplicate of javascript - dynamic variables Commented Dec 11, 2013 at 18:50
  • 2
    There are no pointers in JavaScript, and you cannot allocate memory manually. Also you cannot (should not) create dynamic variable names. Just use the image array that you already have! Commented Dec 11, 2013 at 18:51
  • For your actual problem, see JavaScript Preloading Images - to preload an image, create an image html element (does not need to be in the document) and assign to the src attribute of it. Commented Dec 11, 2013 at 18:52
  • Don't use <!-- ... //--> unless you really, really must support Netscape 2.0 Commented Dec 11, 2013 at 19:40

1 Answer 1

0

You can't create a dynamic variable name. But there's plenty of other solutions.

Why not create an Object that contains your image values?

var images = {};

for(t=0; t<l; t++) {
  images[image + t] = image[t];
}


console.log(images);
> { 
    image0: "http://i.imgur.com/OaElB10.jpg",
    image1: "http://i.imgur.com/NTYiEB9.jpg",
    image2: "http://i.imgur.com/X1jreGc.jpg"
  }

By the way, don't use eval :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.