7 Replies Latest reply on Oct 8, 2007 7:15 AM by Greg Dove

    Pass Array From ActionScript to JavaScript

    Anita Kilgore
      Hi, All:

      Can anyone explain how to pass an array into JavaScript using the getURL function? Current code looks something like this, but it does not work:

      getURL("javascript: myFunc(" + myArray + ");")

      However, this call seems to produce this actual code in both IE and Firefox:

      getURL("javascript: myFunc([object Object]);")

      Both browsers choke on [object Object]. In Firefox, it explicitly thinks some other delimiter should appear around [object Object].

      I searched the knowledge base and it says that arrays can be passed between JavaScript and Flash without any problem, so I am not sure exactly what we re doing wrong in this call. Any help would be appreciated. Thanks!

      AJK
        • 2. Re: Pass Array From ActionScript to JavaScript
          Greg Dove Level 4
          kglad's code will produce a string version of [Object object] which is fine if you want your javascript function to receive the string "[Object object]" . But I think you actually want to pass the array itself. In order to do that you need to serialize it.
          If its a simple array (ie. strings or numbers only - and you know its always going to be like that) then you can just use the String.join method and do a String.split in your javascript function to convert it back to an array.

          For more complete and flexible transfer of complex data types, ExternalInterface does this for you (although it does have some bugs).
          ExternalInterface is probably the easiest way.
          If you must use getURL, then another way to do it using getURL would be to use an existing 3rd party json class in actionscript. Check out json.org (patrick minneault has a tweaked version somewhere too). You would use the stringify method of these classes to convert your array to a json string which you could pass instead of myArray in your getURL and it would be eval'ed correctly in javascript.



          • 3. Pass Array From ActionScript to JavaScript
            Greg Dove Level 4
            Actually my guess is that your array contains a single element which is an object - otherwise it should give you a comma delimited list of the simple data in your example... so you'll need to use externalInterface or the json approach.

            EDIT: Just re-read your post. You said 'associative array' - which is not much different from passing a regular flash object (I suspect what you have is a regular flash object and not a flash array - its possible but not recommended to use flash arrays with non-numeric 'keys').
            • 4. Pass Array From ActionScript to JavaScript
              Anita Kilgore Level 1
              Thank you both for responding.

              GWD, are you saying that I cannot pass an associative array out of Flash? And when you say Flash object as opposed to Flash array, is there some other way I should be creating the array aside from the following example code:

              var myArray = new Array();
              var myVal = 6;
              myArray["aname"] = myVal;

              This associative array contains a normal array within it:

              var myList = new Array();
              myList[0] = 1;
              myList[1] = 2;
              etc...
              myArray["alist"] = myList;

              Does having another array in my associative array complicate things?

              Lastly, do you have any example code using the ExternalInterface? We are using getURL because it is part of existing code. If there is a more efficient way to pass objects of this type between Flash and Javascript, it would be great to understand it and implement it.

              Thanks!

              • 5. Re: Pass Array From ActionScript to JavaScript
                Greg Dove Level 4
                The Array class in flash is best used for numeric indexed arrays only.
                If you want an associative array you're better off using a generic Object.

                Using your code it would be:
                var myObj = new Object();
                var myVal = 6;
                myObj["aname"] = myVal;

                var myList = new Array();
                myList[0] = 1;
                myList[1] = 2;
                etc...
                myObj["alist"] = myList;

                One way of thinking about the javascript in a getURL call is as if it is being eval'ed by javascript. So it needs to be a valid javascript string. When you concatenate a complex data type in a string in flash, its toString method is called. For an Object that means it normally looks like "[Object object]" - so you need to have some way to serialize it into a format suitable for passing to javascript. ExternalInterface does that for you automatically (via XML serialization/deserialization). The other way I mentioned was implementing a json class which converts the data into a format that should work just in a getURL call. But that is not native to flash. So ExternalInterface is easier.


                For ExternalInterface, use something like this:

                if (ExternalInterface.available) {
                ExternalInterface.call("myFunc",myObj);

                } else {
                //javascript interface not available...do something else

                }
                • 6. Re: Pass Array From ActionScript to JavaScript
                  Anita Kilgore Level 1
                  Thanks, GWD. We'll give that a try and report back on how it worked.

                  Prior to posting here, I suggested that we try using eval within the getURL, like this:

                  getURL("javascript: myFunc('" + eval(myArray) + "');")

                  Haven't had a chance to try it yet, but do you think it will work?

                  Thanks again. This is great information!


                  • 7. Re: Pass Array From ActionScript to JavaScript
                    Greg Dove Level 4
                    You're welcome and no... That approach won't work.
                    FYI the eval in actionscript is not like eval in javascript.

                    What you're trying to put in the getURL is a string that would eval successfully in javascript.
                    There is nothing native in flash that will do that for you for a complex data type in a getURL javascript call (like I said earlier you would need to serialize your data with a JSON class i.e. to convert it to a string value that works in javascript - which ends up being not too distant from actionscript source code for an object representation).