views:

31

answers:

1

In the following example (yes, I am coding on my timeline while I try to work this out - I know, I know) I am loading an SWF in an HTML page and then directing the SWF to get the query parameters from the current URL. The query parameter will contain the source for the video to play.

This seems straight forward to me but I cannot get myURL = urlVars.videoloc; to work. More specifically, urlVars.videoloc seems to be undefined rather than holding the query parameter from the URL. All other variables are correct; both wholeURL and urlVars are defined.

//Initialize Global Event Listener
player.addEventListener(Event.ADDED_TO_STAGE, getPlay, false, 0, true);

//Function to play the video
function getPlay(e:Event):void {
    var wholeURL:String = ExternalInterface.call("window.location.search.toString");
    var urlVars:URLVariables = new URLVariables(wholeURL);
    var myURL:String = urlVars.videoloc; //<--- Trouble, returning 'undefined'
    errorBox.text = "videoloc="+urlVars.videoloc+"\nwholeURL="+wholeURL+"\nurlVars="+urlVars+"\nmyURL="+myURL; //<--- The reason I know it is returning 'undefined'

    if (myURL) {
        player.load(myURL);
        player.play();
    }
}
+2  A: 

Ideally you should use a debugger to inspect the makeup of your URLVariables object.

If you're unable to do things the easy way, you could do this to trace its contents:

for (var parameter:String in urlVars) {
    trace(parameter + "=" + urlVars[parameter]);
}

As you can see, you can step through every parameter inside urlVars using a for in loop.

I'm guessing videoLoc is your first parameter? Look at the results of this test of mine:

var address:String = "http://www.google.ca/search?q=test&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-GB:official&amp;client=firefox-a";
var urlVars:URLVariables = new URLVariables(address);

for (var parameter:String in urlVars) {
    trace(parameter + "=" + urlVars[parameter]);
}

The output of this is:

aq=t
rls=org.mozilla:en-GB:official
client=firefox-a
http://www.google.ca/search?q=test
ie=utf-8
oe=utf-8

See what happened to the q parameter? To fix this, use only the text past the ?

var address:String = "http://www.google.ca/search?q=test&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-GB:official&amp;client=firefox-a";
var urlVars:URLVariables 
    = new URLVariables(address.substr(address.indexOf('?')+1));

for (var parameter:String in urlVars) {
    trace(parameter + "=" + urlVars[parameter]);
}
Gunslinger47
Thank you for boiling that down in such an easily to grasp manner.I thought that my use of URLVariables was incorrect - I guess to some extent it was. Extracting the substring after the question mark did the trick.Side note - Using the debugger but wasn't having much luck with getting useful output. Need to work on that.
Structure