Jump to content
Search Community

XML Loader and URL parameters? Can't get it working

Mr Pablo test
Moderator Tag

Recommended Posts

I am wanting to pass along URL parameters inside my XML document to the SWF assets I am loading.

 

Here is the code I am using:

 

XML document

 

 

<data>
    <LoaderMax name="dynamicLoaderMax" load="true" scaleMode="stretch">
        <SwfLoader name="test" url="test.swf?ct1=testing" autoPlay="false" />
    </LoaderMax>
</data>
 

Inside test.swf, frame 2 (frame 1 is "stop();")

 

 

import flash.events.Event;


Security.allowDomain("*");
Security.allowInsecureDomain("*");


var loaderParams:Object = new Object();


addEventListener(Event.ADDED_TO_STAGE, getParams);


function getParams(e:Event):void{
    loaderParams = LoaderInfo(this.root.loaderInfo).parameters;
}


var text1:String = loaderParams.ct1;
var text2:String = loaderParams.ct2;
var text3:String = loaderParams.ct3;
var text4:String = loaderParams.ct4;
 

I also tried:

 

 

import flash.events.Event;


addEventListener(Event.ADDED_TO_STAGE, getParams);


function getParams(e:Event):void{
    var loaderParams:Object = this.loaderInfo.parameters;
}
 

Nothing works for me.

 

Tried adding in traces inside the SWF, I get "undefined"

 

Really need to be able to pass variables along to my SWFs!

 

EDIT - Just to note, despite previous posts, I am now using LoaderMax inside a Flex Mobile application, thought I'm sure this shouldn't affect anything?

Link to comment
Share on other sites

One of the challenges here has to do with the fact that Flash won't load local files properly if the URL has a query string attached (like the "?ct1=testing" on the end of yours). That only works over a network from a server. That is most likely what's causing yours to fail. 

 

I have attached an updated version of LoaderMax (along with the rest of the core GreenSock platform files) that will check to see if you're loading locally and as long as "allowMalformedURL" isn't true (it's false by default anyway), it will switch the URLRequest's method to "POST" in order to prevent local failures when parameters are associated with a request. This is only necessary when you're passing parameters. This update should help things work better locally, but you still have the issue of getting the parameters from inside your subloaded swf, and since it's impossible (from what I can tell) to have parameters in the URL when loading locally, and we're forcing the method to "POST", it won't work to just look for this.loaderInfo.parameters. However, I've got a solution...

 

Here is a method that you can put inside that subloaded swf and it will look for any loaderInfo.parameters AND it will look up the chain to find the ContentDisplay object that has a reference to the original SWFLoader and it'll dig into its URLRequest to analyze the parameters there and then it'll put anything it finds into a generic object for you to make it very simple for you to look up any parameter:

 

function getParams():Object {
    var map:Object = {},
        params:Object = this.loaderInfo.parameters,
        p:String,
        curParent:DisplayObject = this.parent;
    for (p in params) {
        map[p] = params[p];
    }
    while (curParent) {
        if (curParent.hasOwnProperty("loader") && curParent.hasOwnProperty("rawContent")) { //ContentDisplay objects have "loader" and "rawContent" properties. The "loader" points to the SWFLoader. Technically it would be cleaner to say if (curParent is ContentDisplay) but that would force ContentDisplay and some core LoaderMax classes to get compiled into the child swf unnecessarily, so doing it this way keeps file size down.
            params = Object(curParent).loader.request.data;
            for (p in params) {
                map[p] = params[p];
            }
        }
        curParent = curParent.parent;
    }
    return map;
}

So, for example, let's say your URL is "test.swf?ct1=testing&ct2=whatever". Inside that test.swf, you could do this:

 

addEventListener(Event.ADDED_TO_STAGE, addedHandler);
function addedHandler(event:Event):void {
    var params:Object = getParams();
    var ct1:String = params.ct1;
    var ct2:String = params.ct2;
    //or to loop through them all and display whatever was found...
    for (var p:String in params) {
        trace("found parameter: "+p+": "+params[p]);
    }
}

The other nice thing is that this doesn't depend on LoaderMax being used at all - it would still work fine if you loaded the test.swf any other way, although LoaderMax solves the problem of figuring out how to get the "illegal" parameters into the swf when you're loading locally. 

 

Does the help?

update.zip

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...