Home > Flash and Actionscript, Flex, Macromedia Flex > How to get URL query-string variables within Flex application

How to get URL query-string variables within Flex application

On flexcoders, someone asked, how to get the URL parameters in a Flex 2.0 application? By URL parameter, I mean the query-string variables, as shown below:

http://yourserver/yourapp.swf?name=Joe&age=22 or http://yourserver/yourapp.mxml?name=Joe&age=22

Note: To get the variables in flex via mxml file, you need server-side compilation of mxml files.

You can get the values of the params using from

mx.core.Application.application.parameters

object, which contains the name-value pairs (hash-map).

For example,

mx.core.Application.application.parameters.name

would return “Joe”, considering above example URL. You can find more information about this in livedocs: http://livedocs.macromedia.com/flex/20beta1/docs/00001300.html

Then someone asked, what if your swf is embedded in custom html wrapper, how to get the query-string params inside swf?

Most of us use ExternalInterface class in Macromedia Flash Player 8(onwards) to invoke some JavaScript functions and extract the value in ActionScript.

I wrote a quick-and-dirty class in ActionScript 3, which would get the values of params from URL query-string for you. You don’t need any extra JavaScript code in HTML. The same logic can be used for any an ActionScript 2.0 running Macromedia Flash Player 8.
QueryString class has following properties:-

  • parameters – an Object which contains the name-value pairs from query-string
  • queryString- String, this contains the entire query-string (url-encoded) name-value pairs.
  • url- String, this returns the complete URL of the wrapper page with query-string.

See demo
Download source files

  • http://lozza.sendu.to Lawrence Lai

    I like your site Mr. Abdul Qabiz. Thank you for your site, its quite the enjoyable read. Jolly good now.

  • http://kenny.azn.nu Kenny Lim

    Jolly well done with the input strings, quite the systemic anomoly. Jolly good.

  • http://arulprasad.blogspot.com/ Arul Prasad

    and in Flash(AS1 & AS2), you could do this by using the _url property.
    Guess many would’ve known that?

  • http://www.abdulqabiz.com Abdul Qabiz

    Arul,
    _url property returns the URL of the SWF. Which is the first case on above post. What I talked about is getting query-string which is passed to the wrapper page.
    f.ex:
    http://foo.com/page.htm?foo=var
    You can get the foo=var name-value pair using above solution.
    -abdul

  • Bret Stevens

    So is the javascript required, or does your AS do all the work internally (if so, nice!)

  • http://www.abdulqabiz.com Abdul Qabiz

    You don’t need any javascript code. AS takes care of everything.
    -abdul

  • Jason

    This is less of a comment and more of an offer. You have no listed email address, so I thought that this would be the best way to get a hold of you.
    Your actionscript was exactly what I’ve been looking for. My friend and I have had this idea for his local record label for quite some time: if he could put a flash movie on the root of his site (his homepage), as visitors come to to the root from other pages for his bands, the flash movie could read the bandID url parameter and write it to a text file. The movie could then use that text file to show a list of the most recent bands visited by displaying the picture associated with that bandID (i.e. http://www.recordlabel.org/bandID01.jpg). Do you think this is possible using flash? If so, would you like to earn some money to develop it? I have no doubt he would pay for your services, and I’d pitch in too cause I’d like to mod it out and use it for some other things. We’re just a couple of kids, but if you’re interested, we could pay you via paypal before the work is started. If not, thanks for your time anyways.

  • http://chanel.alkablog.com Chanel Rachel

    if it were PHP it would be much easier (there’s a special function for it). But we have what we have (thanks for the post nonetheless)

  • Chris

    Can this work in Mx 2004?
    thxs

  • http://www.abdulqabiz.com/blog Abdul Qabiz

    Yeah, you can make it work in Flash Player 7. Using Flash JavaScript gateway or my JSCommunicator Library insted of using ExternalInterface.
    You can use similar technique.
    -abdul

  • http://Http://ControllingTheInter.Net Keld

    So is there any way to get the Variables from the Present URL with nothing else execpt for flash. You think that it would have it implamented but it dosent. Or does it? Is there a way to retrive it without any Java or anything more like for instants MYSPACE filters out Java :-\

  • http://www.abdulqabiz.com Abdul Qabiz

    hmm, if you are using Flash Player 9, then you should be able to do that.
    If you are using JavaScript (SWFObject) to embed Flash application in your html page, then you should be able to do that and pass things via FlashVars.
    -abdul

  • http://www.abdulqabiz.com Abdul Qabiz

    hmm, if you are using Flash Player 9, then you should be able to do that.
    If you are using JavaScript (SWFObject) to embed Flash application in your html page, then you should be able to do that and pass things via FlashVars.
    -abdul

  • http://www.katoey-blog.com katoey

    But Ive only got flash player 8, what can I do ?
    Katoey

  • jon

    I was able to simplify the conversion method (still compatible with ActionScript 2.0) by using String.fromCharCode():

    public function convert
    (src:String):String {
    while(true) {
    var i:Number = src.indexOf(“%”);
    if (i==-1)
    break;
    var n:Number = parseInt(“0x”+src.substr(i+1,2));
    src = src.substr(0,i) + String.fromCharCode(n) + src.substr(i+3);
    }
    return src;
    }

  • jon

    Ah, fixed a minor bug: if there was a “%25″ on the querystring, that would cause problem. Try “tester.html?text1=abc%25def” with the previous version and you would get the character represented by “%de”!.

    My quickie fix is below:


    private function convert(src:String):String {
      var i:Number = src.indexOf(“%25″); // look for %25, which is the percent-sign
      if (-1 != i) {
        // if we convert a percent-sign in-line, that will confuse
        // our simple parser. Let’s replace it with a sentinel.
        src = src.substr(0,i) + “[Yz” + src.substr(i+3);
      }
      
      while(true) {
        i = src.indexOf(“%”);
        if (i==-1)
          break;
        var n:Number = parseInt(“0x”+src.substr(i+1,2));
        src = src.substr(0,i) + String.fromCharCode(n) + src.substr(i+3);
      }
      
      i = src.indexOf(“[Yz”); // look for sentinel
      if (-1 != i) {
        src = src.substr(0,i) + “%” + src.substr(i+3);
      }
      
      return src;
    }

  • http://www.wendelmaques.com.br wendelmaques

    Thank’s for you function.

  • http://www.digitalmesh.com Anz

    Thanks my friend.. :)

  • Tesla

    Hi, is the flashboject.js file required? If yes, then this statement isn’t entirely correct:
    “You don’t need any extra JavaScript code in HTML.”
    Embedding a JS file is equivalent to having extra JS code in your HTML.

  • Tesla

    I just downloaded and tested out your demo. It looks like the JS file is not required, which put a big smile to my face. Thanks, it’s a very useful class and if I ever use it you will be given credit. Good job.

  • mico

    ANZ, flashobject.js is needed only for including flash object.

  • Xplane

    Thank’s very useful.

  • Jason Hanson

    Has anyone found a way to get the URL when allowScriptAccess=”never” (like on MySpace.com).
    ExternalInterface.call() throws a SecurityError: Error #2060
    The code below always throws an error when embeded on MySpace.com, or a similar site that sets allowScriptAccess to never.
    CODE
    /CODE

  • http://www.abdulqabiz.com/blog/ Abdul Qabiz

    > ExternalInterface.call() throws a SecurityError:
    > Error #2060
    That is intentional to have more security. Myspace (or alike sites) have faced serious security threats in past…Cross-site-scripting stuff…
    I am not sure, if there is a way…
    -abdul

  • Jason Hanson

    Thanks for the reply Abdul. I have been trying to find a work around to this for a few days straight now. The interesting thing is that the Flash player *knows* what page it is on. When you attempt to run any ExternalInterface.call() inside a try / catch the error that is thrown contains a message similar to: “Sandbox security error: The swf at domain http://jason.com cannot access the page at http://www.myspace.com.
    When I found this I was happy and just parsed through the error message with RegEx to pull out the URL. (I only need the URL for tracking purposes – I want to know what sites my SWF ends up on). However, and this is a big however, the error message ONLY contains the stack trace with the URLS of the error when in the Debugger Player.
    This does not work in the retail consumer version of the player. Flash player knows the URL of the page it is on even when allowScriptAccess=”never”. I just want to find a way to capture that error message or some other way to get the page it is on – or even just the domain it is in.
    Thanks for any ideas you have!
    Also, I have been looking for the source of ExternalInterface.as so get a better idea of how to intercept the error it throws. Do you know where I could get that – it is in the playerglobal.swc.
    thanks a ton!
    –jason

  • http://blog.andrewpaulsimmons.com Andrew Paul Simmons

    I just need to get the domain name. I will have user embedded content so I can’t use any JavaScript.

  • rob capilli

    Thank you so very much for posting this and giving us the ability to download your code. It has helped me very much. I will be visiting your site much more.

  • http://www.abdulqabiz.com/blog Abdul Qabiz

    @jon: Thanks for code changes and suggestions.
    @jason, Andrew: I am not able to think total client-side (actionscript/flash) find the domain/page name that embeds SWF, if allowScriptAccess is not allowed. However, I can tell you other way that requires a server-side script…
    <embed src=”http://yourserver.com/getSWF.php” .. />
    Where, getSWF.php might return something like this:-
    yourapp.swf?referer=
    If you serve your SWF through a PHP file, you can capture HTTP_REFERRER on server-side and pass it via query-param of SWF (if it has to go to SWF) or store in DB to track who is referring your APPS.
    Does it make sense?
    -abdul

  • Darren

    Can you contact me if you are your compnay is doing Flex development? I am a developer and I need assistance getting some things in Flex to work even though they are things that should just work.

  • Mahesh

    Hi Abdul,
    I’m new to flex so please bear with me. I have a static HTML page that embeds Flex swf file and I pass one parameter to it.
    like:


    Test.swf?id=567

    both in embed src and movie value.
    I tried your example but it does not seem to work for me.
    Any help is highly appreciated.
    Thanks,
    Mahe

  • http://abdulqabiz.com/blog/ Abdul Qabiz

    @Mahesh: QueryString class is not for getting there parameters passed to SWF via FlashVars or as query-string (as you mentioned). There is built-in support for your use-case. If you are using Flex, you can look at Application.application.parameters object for such properties, if you are using Flash, you can get the query-string passed as properties in stage – AS3 and _level0 – AS2….
    Does it make sense?
    -abdul

  • Subrat

    Hi abdul,
    i am new to flex. in my application i am just passing the url inside navigateToURL ie var urlRequest:URLRequest = new URLRequest(“http://localhost:8080/MyApp.swf?securityId=7″);
    navigateToURL(urlRequest,”_self”);
    .and when MyApp.swf loads in the creationComplete i am trying to access the securityId by mx.core.Application.application.parameters.securityId.
    but this shows the result as “undefined”.
    please help.

  • DaDDeL

    Wow. Never thought of doing it with ExternalInterface.
    Simple, yet effective.
    Thanks a ton!

  • Kinsley

    For all of you that can’t get it to work, make sure that you param “allowScriptAccess” in your html embbeding flash is set to “always” if testing from your computer. it should work with “sameDomain” if tested from your server.

    And just a thought to eliminate all string parsing, you could just use URLVariables to decode the querystring. so this code:

    {
    _params = {};
    try
    {
    _all = ExternalInterface.call(“window.location.href.toString”);
    _queryString = ExternalInterface.call(“window.location.search.substring”, 1);
    if(_queryString)
    {

    var params:Array = _queryString.split(‘&’);
    var length:uint = params.length;

    for (var i:uint=0,index:int=-1; i
    {
    var kvPair:String = params[i];
    if((index = kvPair.indexOf(“=”)) > 0)
    {
    var key:String = kvPair.substring(0,index);
    var value:String = kvPair.substring(index+1);
    _params[key] = value;
    }
    }
    }
    }catch(e:Error) { trace(“Some error occured. ExternalInterface doesn’t work in Standalone player.”); }
    }

    would look like this:

    private function readQueryString():void
    {
    _params = {};
    try
    {
    _all = ExternalInterface.call(“window.location.href.toString”);
    _queryString = ExternalInterface.call(“window.location.search.substring”, 1);
    _params = new URLVariables(_queryString);
    }catch(e:Error) { trace(“Some error occured. ExternalInterface doesn’t work in Standalone player.”); }
    }

    and dont forget to import flash.net.URLVariables and to change the Object to URLVariables for _params.

    Really neat class though. very useful.

  • http://www.abdulqabiz.com/blog/ Abdul Qabiz

    @Kinsley: Thanks, when I wrote this code I am not sure URLVariable class was there, I wrote it for first public beta of Flex 2. I would update the code to avoid loop, though URLVariables would be doing the same internally.
    -abdul

  • guru

    Hi any one help me. How i fetch current URL of the HTML page in flash widget.

  • http://www.abdulqabiz.com/blog/ Abdul Qabiz

    @guru: Since html page is in different domain, this code might not work because it requires html and swf in same domain or allowScriptAccess=always|sameDomain.
    However, you can find about the html page using HTTP referer. Serve your flash-widgets through a server-side script, capture the header and pass it to SWF using flashvars or query string..
    http://yourserver.com/getWidget.php?id=foo
    Above URL can do a HTTP 30x redirection to
    http://yourserver.som/widget.swf?referer=http://otherserver.com/somepage.html
    http://otherserver.com/somepage.html is embedding the SWF..
    Does that make sense?
    -abdul

  • http://www.flickrmailer.com Anz

    Hi Abdul,
    I think the URLVariables solution given by DaDDeL is more good because it will automatically take care of url decoding.
    Thanks for the structured and uselful class..
    Best Regards
    Anz

  • gena

    how do i do the exact opposite of what you did. i need to send a url-encoded string to a server. i can’t get the variable to be a string…it deciphers my “&” and “=” into %3d etc.
    i use URLVariables, URLRequest, and a URLLoader?? i don’t need to load anything…just POST some info in a URLencoded string.
    THANKS!

  • http://tom,kulturlounge.de tom

    Oooh Abdul!
    Do you remember the axe advertising???
    Of course you do.
    Like it ;)

  • Fahad

    dude this site rox !
    found asnwers here that that cudnt anywhere
    Good job , keep the site running :D

  • Jason

    Did anyone ever get around the flash allowScriptAccess problem?

  • Anoop Sharma

    Hello.
    Need a bit help, I am setting a variable in flex application. and pasing a xml file name from flashvars from HTML. If I direct give the file name it shows the data in gridtable but while passing it is not taking. here is the code::
    Code for MXML file ::
    —-
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    —-
    code for HTML ::
    -
    -
    -
    -
    -
    -
    -
    -
    - —-

  • Justin

    link to adobe live is broken

  • http://www.abdulqabiz.com/blog Abdul Qabiz

    @Justin: Thanks, this article was posted during Flex 2 Beta time.. Link is down, I would correct it.
    -abdul

  • Anoop

    Hello Abdul.
    I read your comments and got several solutions from here. I stuck again, the problem is:
    when I give a XML file name with path (ie: xml/tableGrid.xml) in MXML it works fine with HTTPService method. But when I am passing the XML file name through Flashvars it is giving error “UseProxy set to flase” which I alerady did. Alert shows the name of teh file correctly, SO passing name from flashvars is not a problem but why I am not getting the data in dataGrid from that XML??
    Also if I put direct xml data in a variable and pass it through flashvars it is showing fine in DtatGrid without even HTTPService method. But I have to pass data from external XML File.
    Where I am doing mistake , can You plese give some comment on this.
    Thx
    Anoop

  • jason

    You can’t get URLs from Myspace/Facebook because of allowscriptaccess never. Please vote to fix the bug below so we have this information everywhere.
    The browser URL information is actually available in the Error Code returned from a call to ExternalInterface (when Javascript is not allowed). This information is not available in the standard player (but is in the debug), ARGH…. We all want to track our Flash widgets so please vote to fix this bug. You would then be able to get the browser URL from the Error code returned on the API call from Myspace page (a bit of a hack but the information would be available).
    http://bugs.adobe.com/jira/browse/ASC-3095

  • Steve

    This site also provides a way to get the url parameters in actionscript: http://www.flexpasta.com/index.php/2008/03/15/getting-url-parameters-host-name-port-in-flex-actionscript/

  • Jigar Oza

    Hi,
    I have downloaded your code.
    But whenever i try to give parameters in url like this :
    “C:\Documents and Settings\jigaro\Desktop\ParamTesting\bin\ParamTesting.html?myName=Jigar&myHomeTown=Ahmedabad”
    I am getting following error :
    Windows cannot find ‘C:\Documents and Settings\jigaro\Desktop\ParamTesting\bin\ParamTesting.html?myName=Jigar&myHomeTown=Ahmedabad’. Check the spelling and try again, or try searching for the item by clicking the Start button and then clicking Search.
    What should i do now?

  • http://blog.circlecube.com Evan Mullins

    Hey Adbul,
    Great Blog, I’ve learned a lot already! I really love this QueryString class you’ve written. Would you mind if I used some of it in a similar how to post for flash but rewriting it for as2?
    I’ll send you a link soon…
    Thanks again and keep it up!