7

So I have a URL that I need my Flash movie to extract variables from:

example link:
http://www.example.com/example_xml.php?aID=1234&bID=5678

I need to get the aID and the bID numbers.

I'm able to get the full URL into a String via ExternalInterface

var url:String = ExternalInterface.call("window.location.href.toString");
if (url) testField.text = url;

Just unsure as how to manipulate the String to just get the 1234 and 5678 numbers.

Appreciate any tips, links or help with this!

Leon Gaban
  • 36,509
  • 115
  • 332
  • 529

2 Answers2

12

Create a new instance of URLVariables.

// given search: aID=1234&bID=5678
var search:String = ExternalInterface.call("window.location.search");
var vars:URLVariables = new URLVariables(search);
trace(vars.aID); // 1234
trace(vars.bID); // 5678
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • woot thanks :) where can I find info on all the possible "window.location. syntax and what they do – Leon Gaban Apr 28 '10 at 14:56
  • 2
    @Leon, I personally like the Firefox/Gecko reference, but there are other references available. https://developer.mozilla.org/en/DOM/window#Properties – Samuel Neff Apr 28 '10 at 15:09
1
var valuePairs:Array = url.substring(url.indexOf("?")+1).split("&");
var map:Object = new Object();
for (var i:int=0; i < valuePairs.length; i++) {
    var nextValuePair:Array = valuePairs[i].split("=");
    map[nextValuePair[0]] = nextValuePair[1];
}


trace(map["aID"]); // 1234

Untested code! This is just with simple string manipulation.

and... almost 1-liner (also untested)

var map:Object = new Object();
var temp:Array;
for each (var i:String in /.*\?(.*)/.exec(url)[1].split("&")) map[(temp=i.split("="))[0]]=temp[1];

but of course it's probably better to go with urlVariables :)

jonathanasdf
  • 2,844
  • 2
  • 23
  • 26