Click to See Complete Forum and Search --> : Passing vars in AS3 without using URL string?
KeithAlan
02-12-2008, 12:03 PM
Hello,
I have one AS3 swf (loader.swf) that needs to load a second AS3 file (main.swf).
loader.swf has some variables that I would like to pass to main.swf.
I don't want to append the variables in the URL string, because then main.swf won't be cached properly.
In AS2 I could use FlashVars, which passes the variables without adding them to the URL string.
How can I do this in AS3?
_Ric_
02-13-2008, 08:56 AM
I don't want to append the variables in the URL string, because then main.swf won't be cached properly.
Not sure if this would work, but you could try adding a random number to the end of the appended variables, to prevent caching problems. It works for passing variables to php scripts - the random number leads the system to believe it is a different php script than the last one and hence doesn't rely on the cached version.
cancerinform
02-13-2008, 10:30 AM
In the javascript in the html you add the variables here:
'movie', 'mymovie?var1=this is var1',
In your movie you get the variable using this:
var myVar:String = loaderInfo.parameters["var1"];
KeithAlan
02-13-2008, 12:00 PM
Maybe I am not being clear. I WANT to cache the second (main.swf) file. But I also want to pass it a parameter, not using the query string. The parameter is pretty much random, but when it's in the URL, it causes browsers to NOT cache the main.swf file, since it thinks it's another request.
_Ric_
02-13-2008, 02:12 PM
Ah - I see. Sorry, I don't know how to do that directly. You could always get the loader to send the variables in a url string to a php script, and have the script save them to a server side text file or database. You could then have the main swf read them in - although it's probably a messy workaround if someone knows of a direct method.
5TonsOfFlax
02-13-2008, 02:20 PM
Load your second movie without params, then call a method in it to initialize from params as arguments. To do this you must cast the loaded movie to its actual class (or MovieClip). You may also have to specify that it should load in the same ApplicationContext.
cancerinform
02-13-2008, 02:42 PM
I found this using javascript (2nd example):
http://www.manuelnegri.com/web_design/flash_tut1.htm
5TonsOfFlax
02-13-2008, 03:09 PM
Keith, if you're still having problems with this, I may be able to throw some code up later tonight. Did my answer help at all, or should I create a proof of concept?
cancerinform
02-14-2008, 01:21 AM
I found out you can use Flashvars. This is in your html-javascript (only a part I wrote here):
'height', '400',
'src', 'passvarjavascript',
'FlashVars','var1=hello',
'quality', 'high',
'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
'align', 'middle',
This is in your movie:
var var1:String=loaderInfo.parameters.var1;
try
{
myText.text=var1;
}
catch (e:Error)
{
myText.text=String(e+": var1");
}
Of course this is only an example. Your movie should stay in cache.
KeithAlan
02-14-2008, 04:41 PM
Hi, 5TonsOfFlax,
I would appreciate it quite a bit if you could show me some AS3 code that loads a swf file with parameters, without passing them in on the query string. I want to ensure that the huge main.swf files are cached and not re-requested every time from our server.
Keep in mind: I cannot change the main.swf file, I don't control it. I can only pass a variable into it, such as "userId". Thank you for the help!
5TonsOfFlax
02-14-2008, 04:56 PM
Ah, I was assuming you had some degree of control over the main.swf. If you don't, then my solution is not practical. You could end up setting variables, but you have no way of making sure they are set when the loaded movie initializes. The flashvars stuff is a good start. I will have to do some fiddling around to see if loaded movies inherit parameters from their parents (if they do, that solves your problem). If not, I'm afraid I don't have a good solution for you.
Edit:
I have an idea about using URLVariables in your URLRequest and sending as POST if the easy way doesn't work. I've no idea whether that will work either. And I cannot work on this until tomorrow at the earliest.
KeithAlan
02-22-2008, 01:39 PM
Thank you. Let me know if you were able to get some POST-based code to work. I appreciate it!
derari
02-23-2008, 11:41 AM
How are you loading that main.swf?
Generally, you can access the loaded swf like a movie clip, that means the container swf can modify all public properties of the loaded file.
mobilhood
02-02-2009, 04:11 AM
has anyone found a solution for this?
loading an external SWF, passing variables to it like FlashVars ,without query string?
EDIT:
sorry for popping up such an old thread...
flashkit.com
Copyright Internet.com Inc., All Rights Reserved.