Access flashvar defined in HTML from child loaded swf? ANSWER PROVIDED BELOW
I have a flash var stored in my html file:
<script type="text/javascript">
var flashvars = {
map:"mapGAcentury21.xml"
};
var params = {
menu: "false",
scale: "noScale",
allowFullscreen: "true",
allowScriptAccess: "always",
bgcolor: "#FFFFFF"
};
var attributes = {
id:"REMap"
};
swfobject.embedSWF("REMap.swf", "altContent", "840", "630", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
</script>
I have a flash application I developed in FlashDevelop. From within this Flash application I load a file called 728x90.swf. I can freely edit my 728x90.fla file but I do not want to change my main flash application files.
How开发者_Python百科 can I access the flashvars variable from within my 728x90.swf file?
EDIT:
The following codes traced "your variable is undefined"
1. trace("your variable is " + stage.loaderInfo.parameters.flashvars);
2. trace("your variable is " + loaderInfo.parameters.flashvars);
3. trace("your variable is " + loaderInfo.parameters.flashvars.map);
4. trace("your variable is " + stage.loaderInfo.parameters.flashvars.map);
EDIT 2: I pasted my entire javascript code into my original post. my flashvars are being passed into my main swf file called "REMap.swf" The REMap.swf
public var requestAd:URLRequest = new URLRequest("media/728x90.swf");
Then in a function the following code loads my 728x90.swf file
loaderAd.load(requestAd);
addChild(loaderAd);
loaderAd.contentLoaderInfo.addEventListener(Event.COMPLETE, promoLoaded);
You can get the parameters of the parent swf by calling
parent.loaderInfo.parameters
Your document object is a MovieClip
. The flashvars associated with the MovieClip are stored in its loaderInfo.parameters
property. Don't get the flashvars from stage.loaderInfo.parameters
unless you want the flashvars associated with the original calling flash movie.
Edit to add:
Read about how to pass flashvars to a flash video. I'm getting the feeling that you're trying to access a JavaScript object from flash. That can be done too, you'll just need to read up on flash.external.ExternalInterface
Edit #2:
public var requestAd:URLRequest = new URLRequest("media/728x90.swf");
should probably be:
public var requestAd:URLRequest = new URLRequest("media/728x90.swf?map=mapGAcentury12.xml");
Check out what HTML is generated by the swfobject.embedSWF
call. It might not be sending the flashvars correctly.
Simple answer:
Create a variable and name it whatever you want. I tried the following three:
var s1:String = stage.loaderInfo.parameters["map"];
var s2:String = this.root.loaderInfo.parameters["map"];
var s3:String = root.loaderInfo.parameters.map;
trace(s1 + " " + s2 + " " + s3);
The output: mapGAcentury21.xml null null
Answer: In order to access flashvars of a parent swf from a child swf use the following code:
var s1:String = stage.loaderInfo.parameters["PLACE THE NAME OF YOUR FLASHVAR WITHIN QUOTATIONS"];
trace(s1);
Phil is correct that:
var s1:String = stage.loaderInfo.parameters["map"];
will work, but it will only work if the child SWF has already been added to the stage. This happens one/some frame(s?) after loading is complete. You can listen the the event in the child like so:
public function instantiateChildClass(){
trace(stage.loaderInfo.parameters["map"]); //throws error if this SWF has just been loaded into a parent SWF
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void{
removeEventListener(Event.ADDED_TO_STAGE, init);
trace(stage.loaderInfo.parameters["map"]); //traces map param
}
精彩评论