5 Replies Latest reply on Jun 20, 2013 6:33 AM by kglad

    Array inside getURL not working

    Cool Colors

      I'm trying to put links on multiple images using the titles of the images that I keep inside an array

       

      var imagesTitles = new Array("Image title1", "Image title2", .... );  

       

      I'm starting a for loop here and after I replace the empty spaces with hyphens and lowercase the image titles I'm trying to attach a link to every instance like this:

       

      getURL(imageLinks[i], "_blank");

       

      If I trace imageLinks it looks like this http://www.mysite.com/image-title


      The browser opens and I get http://www.mysite.com/undefined

       

      What would be the correct way to use getURL with an array?

        • 1. Re: Array inside getURL not working
          kglad Adobe Community Professional & MVP

          those strings are not valid urls. 

           

          check urlencoding or, to encode a space use %20.  for example:

           

          var imagesTitles:Array = new Array("Image%20title1.jpg", "Image%20title2.gif", .... );  

           

          or, using underscores instead of spaces can work but you still need a file extension. 

           

          in any case, if you think imageLinks[0], for example, is a valid url, use the trace() function to confirm.

           

          if it is a valid url, the url is probably incorrect or you have a security sandbox issue.

          1 person found this helpful
          • 2. Re: Array inside getURL not working
            Cool Colors Level 1

            Here is my code:

             

            String.prototype.replace = function(s, r) {

                return this.split(s).join(r);

            };

            for (var i:Number = 0; i < imagesTitles.length; i++) {

            imageLink[i] = imageTitles[i];

            imageLink[i] = imageLink[i].replace(" ", "-");

            imageLink[i] = imageLink[i].toLowerCase();

            imageLink[i] = "http://www.mysite.com/" +imageLink[i] + "/";

            trace (imageLink[i]); // trace here shows a list of valid urls

            gallery_mc["image" + i.toString()].onRelease = function(){

            trace (imageLink[i]); // trace here shows undefined

            getURL(imageLink[i], "_blank");

            };

            }

             

             

            Must be a scope problem but I just started using AS2, I don't know how to fix it.

            • 3. Re: Array inside getURL not working
              kglad Adobe Community Professional & MVP

              that code is problematic: if there's more than one string in imageNames and your code worked you would be opening multiple popup windows simultaneously.  fix that.

               

              once that's done, copy and paste the first trace output line.

               

              if that's really a valid url, also upload your html and swf (and images) to your server and test there. 

               

              if that doesn't work, post a link to your embedding html

              1 person found this helpful
              • 4. Re: Array inside getURL not working
                Cool Colors Level 1

                Thank you for for your answers they helped me to make a better analyze of my code and:

                 

                I think I figured it out finally after a serious number of trial and error. The problem was that I was trying to use the i variable in a different scope. Tracing the i variable inside the onRelease was my key clue. I was trying to use imageLink[i]  but i doesn't exist inside onRelease hence undefined url.

                 

                To keep it short, the key was to declare a variable for each image movieclip that is inside the gallery movieclip like this:

                 

                for (var i:Number = 0; i < imagesTitles.length; i++) {

                    gallery_mc["image" + i.toString()].k = i; 

                }

                 

                after this I can now use inside onRelease inside getURL imageLinks[this.k], imageLinks being the array with the complete list of valid urls.

                • 5. Re: Array inside getURL not working
                  kglad Adobe Community Professional & MVP

                  you're welcome.

                   

                  p.s. i missed that onRelease line of code.  in the future, formatting your code makes it easier to detect lines like that.