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
Tagged with:
 

8 Responses to Loading URL encoded variables into a Flash application using the URLLoader class in ActionScript 3.0

  1. bhoser says:

    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…..

  2. me says:

    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.

  3. 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?

  4. ashish says:

    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

  5. Peter deHaan says:

    @ashish,

    You want to use “FlashVars” and loaderInfo.parameter. For example, assuming we have the following ActionScript 3.0 code:

    import flash.text.TextField;
     
    var tf:TextField = new TextField();
    tf.x = 20;
    tf.y = 20;
    tf.width = 200;
    tf.height = 100;
    tf.border = true;
    addChild(tf);
     
    var key:String 
    var params:Object = loaderInfo.parameters;
    for (key in params) {
        tf.appendText(key + ": " + params[key] + "\n");
    }

    And then you’d need to modify your HTML code to pass some variables to the ActionScript code, as seen in the following snippet:

    <param name="flashvars" value="foo=bar&name=peter&site=actionscriptexamples.com" />

    And your SWF file should display the following text in the text field:

    site: actionscriptexamples.com
    foo: bar
    name: peter

    Peter

  6. Alis says:

    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

  7. Alis says:

    param name=”flashvars” value=”foo=bar&name=peter&site=actionscriptexamples.com” /

  8. Nice tips, make it more advanced.

    Thanks
    Ravi Bhadauria

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Spam Protection by WP-SpamFree