I want to specify a string value in the HTML containing my Flash created using Flex 3. This value is a URL that is used by the Flex code and I开发者_高级运维 want another dev to be able to update it. How do I do this? I'm using Flex Builder 3.
You could try javascript and ExternalInterface or just use flashVars
If you just have the one parameter you'd like to pass in, it's probably easier to use flashVars.
FlashVars would be the easiest solution for this.
Embed code for SWF:
<object width="300" height="250">
<param name="flashvars" value="var1=hello&var2=world" />
<embed src="file.swf?var1=hello&var2=world" width="300" height="250" />
</object>
Accessing vars from AS3:
var ext:Object = root.loaderInfo.parameters;
// ext.var1 -> hello
// ext.var2 -> world
As Alex Jillard and Marty have pointed out, you can use flashvars
to pass data to a Flash application. This approach however, is only viable if the data you wish to pass along exists at the time the application loads.
If you wish to pass data to a Flash application at any point during the application's lifetime, you can do so by creating a method in the application for such a purpose, adding the method to the application's ExternalInterface
(which Alex mentioned), and then calling that method with Javascript.
Assuming the Flash application is represented in your HTML as a DOMElement named flashDOMElement
, and the aforementioned method is named acceptData
, you can add the method to the application's external interface like so:
//ActionScript
ExternalInterface.addCallback("acceptData");
And call the method using the DOMElement like so:
//Javascript
flashDOMElement.acceptData(/*arg1, arg2, ... */);
Your reason behind wanting to do this (making the data available for another dev to modify) leads me to believe that you may want to persist such data once the Flash application receives it. You can do so with Locally Shared Objects.
If my assumption is correct, BakedGoods is a solution you may want to look in to. Its a Javascript library that establishes a uniform interface that can be used to conduct common storage operations in all native, and some non-native storage facilities, including Flash Locally Shared Objects.
With it, creating an LSO can be accomplished with code as simple as:
bakedGoods.set({
data: [{key: "key", value: "value"}],
storageTypes: ["flash"],
complete: function(byStorageTypeRemovedItemKeysObj, byStorageTypeErrorObj){/*code*/}
});
For the sake of complete transparency, BakedGoods is maintained by none other than this guy right here :) .
精彩评论