开发者

How to assign URLVariables result to a String Variable?

开发者 https://www.devze.com 2023-01-14 03:54 出处:网络
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

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 E开发者_如何学JAVAvent 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();
    }
}


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&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&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&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a";
var urlVars:URLVariables 
    = new URLVariables(address.substr(address.indexOf('?')+1));

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

精彩评论

暂无评论...
验证码 换一张
取 消