2

Is it possible to pass native JavaScript objects such as arrays and hash-map-like objects to Flash Player with SWFObject?

I basically need to pass an array of objects to Flash from JavaScript in the Flash variables, so this is my code:

swfobject.embedSWF("application.swf", "divvy" "100%", "100%", null,
    { 'info': [
        { 'id': 1, 'name': "Hello, John" },
        { 'id': 2, 'name': "Hello, Dave" }
    }]);

When I get the object in Flex, I simply get a String that looks like this: "[Object object], [Object object]" which leads me to believe that my array is being serialized into a String before entering Flash. Is there any known workaround?

And yes, I do need to pass Flash variables in this manner, since they'll key off some loading which is necessary to do before the application starts.

2
  • 1
    You don't have to pass Flash variables in that manner, and in fact you can't, at least not in the way you show here. What you can do is let it connect to the server and get the data before you let the app begin running. Show a progress bar until your data arrives. Commented Nov 10, 2010 at 21:27
  • Dead on, thanks. I forgot that Flash vars are simply URL variables, not actual JavaScript objects Commented Nov 10, 2010 at 21:45

3 Answers 3

6

Use the JSON lib from as3corelib and pass the entire object encoded as a json object and it will become a flash object once its deserialized. You might need to urlencode the json string to pass it as a string, once it gets into as3 it will be de-urlencoded, and you can unserialize it using the as3corelib's json function, and you will have a object in flash that represents your data.

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

Comments

2

The complex object must be sent as a string via flashvars, you could then use as3corelib's JSON parser to read it.

Alternatively you could use ExternalInterface to call a javascript function to return the object as is.

As was also suggested in the comments, you could have Flash request the data from the server itself, there's a lot of ways this can be done.

Comments

0

Answer: as @Robusto mentioned above it is not possible to pass native JavaScript objects to Flash Player via Flashvars. I forgot that Flash variables are simply GET parameters to the SWF, nothing more. Thus, application.swf?r=123 is the same thing as swfobject.embedSWF('application.swf', '100%', '100%', null, {'r': 123}, null);

I'll probably just load some XML or something.

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.