The following example shows how you can load an external text files with URL encoded variables using ActionScript 2.0 and ActionScript 3.0.
Full code after the jump.
The following example shows how you can load an external text file with URL encoded variables into an ActionScript 2.0 document using the LoadVars class:
// ActionScript 2.0 var loadVars:LoadVars = new LoadVars(); loadVars.onLoad = function(success:Boolean):Void { if (success) { lbl.text = loadVars.lastName + "," + loadVars.firstName; // Jones,Tom } else { lbl.text = "unable to load text file."; } } loadVars.load("params.txt");
Note that this time ActionScript 2.0 is using the onLoad event handler instead of the onData event handler. The difference? Well, onData gets called before the loaded text is parsed, whereas onLoad gets called after onData and after the text has been parsed into variables.
You may recall from the previous entry, “Loading text files using the URLLoader class in ActionScript 3.0″, that the onData event handler receives a single String parameter, which is the raw unparsed text. The onLoad event handler receives a single Boolean parameter, which reports whether the text was able to be parsed successfully or not.
The following example shows how you can load an external text file with URL encoded variables into an ActionScript 3.0 document using the URLLoader class and setting the dataFormat property to the URLLoaderDataFormat.VARIABLES constant (“variables”):
// ActionScript 3.0 var urlRequest:URLRequest = new URLRequest("params.txt"); var urlLoader:URLLoader = new URLLoader(); urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES; urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete); urlLoader.load(urlRequest); function urlLoader_complete(evt:Event):void { lbl.text = urlLoader.data.lastName + "," + urlLoader.data.firstName; }
And what, exactly, does this “params.txt” text file look like?
firstName=Tom&lastName=Jones
8 Responses to Loading URL encoded variables into a Flash application using the URLLoader class in ActionScript 3.0
Leave a Reply Cancel reply
Recent Posts
- Getting started with the TLFTextField class in ActionScript 3.0 and Flash CS5
- Adding tick marks to a Slider control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Creating a custom context menu item in Flash using ActionScript 3.0
- Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10
- Detecting when the user changes the color in a ColorPicker control in Flash using ActionScript 3.0
- Getting the currently selected color as a hexadecimal value on a ColorPicker control in Flash using ActionScript 3.0
- Toggling the text field on the ColorPicker control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Setting the number of columns on a ColorPicker control in Flash using ActionScript 3.0
Categories
- Bitmap (1)
- Components (72)
- Button (19)
- CheckBox (2)
- ColorPicker (6)
- ComboBox (1)
- DataGrid (8)
- FLVPlayback (7)
- Label (9)
- ProgressBar (2)
- Slider (3)
- TextArea (1)
- TextInput (7)
- UILoader (7)
- ContextMenu (1)
- Embed (4)
- ExternalInterface (2)
- Flex (7)
- Font (2)
- General (5)
- Graphics (2)
- JSFL (14)
- Loader (3)
- LoadVars (3)
- Microphone (1)
- migration (12)
- MovieClip (1)
- MovieClipLoader (1)
- Sound (2)
- TextField (1)
- TLFTextField (1)
- TransitionManager (1)
- Tween (1)
- Uncategorized (1)
- URLLoader (4)
- URLVariables (1)
- Video (1)
- XML (2)
Advertising


I am migrating from LoadVars in Flash 8 to CS4 AS3 URL* classes. What got me originally was how exact the data you are loading has to be… For instance with LoadVars you could use “&var1=data1&&var2=data2&” or “var1=data1&var2=data2&”. I have tried multiple ways to get this to work with AS3 in CS4. The only way I found to get it to work which is posted above is….
“var1=data1&var2=data2″ works!!!
“var1=data1&” will give a #2101 error
“var1=data1&var2=data2&” will give a #2101 error
to iterate I used this for loop
for(var prop in loader.data){
trace(prop + “=” +unescape(loader.data[prop]));
}
Hope this helps someone out there…..
Yes, you have to remove the & to works. I lost 4 hours to find this solution. This is very stupid i think adobe developers are the same.
Hey, i need help with this and ive been looking where to post this but cant find where, pretty new to this. I d really appreciate your help. i need to load a url request using a variable in between its name , like this:
big_pic.load(ArtistImg[numberVariable]Req);
but its not wokring, does anuybody know how to pass a
variable in that situation?
sir i want to know please tell me a complete tutorial about how i get veriable value from published html file and what are the codes used in flash file
@ashish,
You want to use “FlashVars” and
loaderInfo.parameter. For example, assuming we have the following ActionScript 3.0 code:And then you’d need to modify your HTML code to pass some variables to the ActionScript code, as seen in the following snippet:
And your SWF file should display the following text in the text field:
Peter
Hi.here I used your code for a html document named ‘Test.html’ but unfortunately it didnt work:
import flash.text.TextField;
var Url:URLRequest = new URLRequest(“Test.html”);
var vars:HTMLLoader = new HTMLLoader();
vars.load(Url);
vars.addEventListener(Event.COMPLETE,onComplete);
function onComplete(evt:Event):void {
var tf:TextField = new TextField();
tf.x = 20;
tf.y = 20;
tf.width = 200;
tf.height = 200;
tf.border = true;
addChild(tf);
var key:String
var params:Object = loaderInfo.parameters;
for (key in params) {
tf.appendText(key + “: ” + params[key] + “\n”);
}
}
and Test.html:
Could you please help me how to fix it?
best regards
Alis
param name=”flashvars” value=”foo=bar&name=peter&site=actionscriptexamples.com” /
Nice tips, make it more advanced.
Thanks
Ravi Bhadauria