Get current url to Flash swf using an External Interface call

Update: please see the newer tut talking about getting the current url with as3


Many have struggled with the task of getting you swf to read or get the current url showing in the browser, the html page the browser is at which has the swf embedded. Not to be confused with the _root._url which returns the path of the swf file. This would be helpful to know if someone is embedding your swfs on their site, or even customize your swf depending on which page it resides on. There is a pretty simple, yet virtually undocumented way to do this. We have to use javascript by calling External Interface and get the url from the DOM. Window.location.href. Then we must call the toString() function only because requires a function rather than only reading a static property.


  1. Import external interface into your file: import flash.external.ExternalInterface;

  2. Initialize a variable to store the url path: var urlPath;

  3. Create a function to call external interface and assign the html page path to your variable: urlPath =“window.location.href.toString”);

  4. Call the function when/if needed.


With javascript: window.location.href or window.location.href.toString();
With actionscript:“window.location.href.toString”);
External Interface html Example

Get Adobe Flash player


import flash.external.*;
var urlPath;

function geturlhttp() {
urlPath ="window.location.href.toString");
//Here I assign the url to a text box on the stage
_root.urlText.text = urlPath;


This entry was posted in tutorial and tagged , , , , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.
  • Jason

    Great but you can\’t get URLs from Myspace/Facebook because of Javascript is not allowed. 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).

  • DB

    this doesn’t work in IE… alot of people having trouble with this :/

  • Evan Mullins

    It is interesting, it seems to have a lot to do with the Microsoft’s dealings with Active content. Clicking on the “External Interface html Example” link does work in IE. I have struggled at getting this to work in IE scenarios also. Any suggestions?

  • agi

    You can also use location.href and pass it through SWFObject to get the current url of a page like:

    so.addVariable(“urlPageName”, location.href);

    I used it a couple of times across projects that needed to work locally and on the server. This worked fine for me across IE and Firefox.

  • James

    @Evan – Thanks for this, learnt something new – just a shame it doesn’t play ball in IE.

    @agi – Good suggestion, I think that’ll work…

  • Ed McManus

    Hi Guys,

    Just thought you might like to know there’s a solution to this problem. The following snippit works across all browsers I’ve tested (tho clearly it requires JS access):

    var result:String =‘eval’, ‘window.location.href’);

    Hope it helps!

  • Kirk Strobeck

    I developed a new simple web tool that uses FScommand from flash and SWFObjecy implementation from the Javascript side. The two pair up nicely to make a functional addressing system in flash.

    Check it out =>

  • Evan Mullins

    I’ve written an updated post on this subject see Get Current URL and Query String Parameters

  • John T. Bailey

    What is the advantage of using the ExternalInterface technique as you’ve described over the following:

    var lc:LocalConnection = new LocalConnection();
    var domain:String = lc.domain;

    • oxygen


      or shorter:

      trace(new LocalConnection().domain)

  • Sander van den Berg

    Hello all!

    @John: Think this will only give the URL of the location of the SWF, it won’t give you the URL of the screen in which you are viewing the SWF.

    for example:
    domain 1: contains SWF
    domain 2: contains html file with embed sourcecode.

    You want the URL of domain 2, so you can track ur SWF.

    This is my issue aswell. Since im gonna work with paying clients, having their own layout. I want to dynamicly get their XML file containing layout info (which is on my server) based on a URL or Domainname send by them automaticly when viewing my SWF.

  • nick

    I’m trying to use this function to insert the current url when people submit a form, the code is below but the import flash.external.*; is created an endless loop how can I fix it, and is the way im getting the url ok or should i just put it into a variable instead of using the function? I appreciate any help


    System.useCodepage = true;
    send_btn.onRelease = function() {

    import flash.external.*;

    function geturlhttp() {
    _root.urlPath =“window.location.href.toString”);

    my_vars = new LoadVars();
    my_vars.url = geturlhttp(); = email_box.text; = name_box.text;
    if ( != “” and != “” and my_vars.url != “”) {

    my_vars.sendAndLoad(“mailer.php”, my_vars, “POST”);
    } else {
    my_vars.onLoad = function() {
    email_box.onSetFocus = subject_box.onSetFocus=message_box.onSetFocus=function () {
    if (error_clip._currentframe != 1) {

  • kavin

    Hi,all’eval’, ‘window.location.href’);
    this can get url,but i have a problem,my flash is a widget and when it insert into a page,this widget has not a id(<object classid=…. id=…) and not work.
    how can i do ?

  • Evan Mullins

    @Kavin – your flash does not need an id or class to use ExternalInterface. Although sites lie MySpace don’t allow the use of javascript, so we can’t use ExternalInterface there. Good luck!

  • kallal

    This used to work fine in older versions during the AS1 days. What broke?

  • Pingback: Get Current URL and Query String Parameters to Flash | Tutorial | circlecube()

  • Adrian

    I tried to use your code below but it is not working in my end.

    The return value is “undefined”.

    I am currently experimenting on how can I get the querystring of the current URL and append it in the url in of my getURL function.

    Below is a sample of my code.

    import flash.external.*;

    function geturlhttp()
    //will contain the Current URL
    var urlPath =“window.location.href.tostring”);

    //trace (urlpath)
    // result is undefined
    //get current URL of the site
    var QStr:String;
    Qstr = “”;
    if (urlPath.indexOf(“?”) -1)
    Qstr = urlPath.substr(urlPath.indexOf(“?”), urlPath.length );

    getURL(“” + Qstr , “_self”);


    _root.LearnD.onRealease = function()


    Whenever I click the LearnD button , I am redirected to this page.

  • Evan Mullins

    @Adrian – this line is most likely giving you trouble:
    if (urlPath.indexOf(”?”) -1)
    should it read:
    if (urlPath.indexOf(”?”) == -1) ?
    let me know if that does it. happy scripting.

  • Adrian

    I correct the codes aand the issue is still existing.

    I am still getting “undefined”. I also noticed that the code is working if I will assign a fixed URL value for the UrlPath variable.

  • Evan Mullins

    Actually, it should be != rather than ==, and remember that variable names are caSe seNseTive, so your QStr and Qstr are not the same things.

    this worked for me:

    function geturlhttp() {
      //var urlPath ="window.location.href.tostring");
      urlPath = "";
      //trace (urlpath)

      var QStr:String = "";
      if (urlPath.indexOf("?") != -1) {
        QStr = urlPath.substr(urlPath.indexOf("?"), urlPath.length );

      getURL("" + QStr , "_self");

  • Keith

    This page claims to provide the URL to the SWF but it does not. The example provided, and all of the alternative examples in the comments, provide the URL to the PAGE containing the SWF.

  • Og2t

    Thanks for the solution, that’s what I was looking for.

  • MicroAngelo


    I don't really like to require users of my swf's to have to add and javascript, so instead I do this entirely in actionscript:“eval”,”document.location.href”);

    You have to use “eval” (evaluate) to work across all browsers (and “document.location” is safer than “window.location” in case you're in a frameset).



  • MicroAngelo


    I don’t really like to require users of my swf’s to have to add and javascript, so instead I do this entirely in actionscript:“eval”,”document.location.href”);

    You have to use “eval” (evaluate) to work across all browsers (and “document.location” is safer than “window.location” in case you’re in a frameset).



  • atasözleri

    You are perfect!

    I am too many time finding it, very nice and wonderful working.

    Thanks again!

  • Hieu

    Thanks a lot for sharing.

  • mouhammad

    thaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks for all so much

    you’ve helped me so so so much

    I’m working on my project and I’ll show after finish what I did !!!!!!!

    thanks again

  • mouhamma

    hello guys

    how can I check urlpath what starts with ??

    i.e :

    I have a link for example :

    and after getting urlpath by :

    var urlPath =“window.location.href.tostring”);

    check if it starts with :

    if true trace(yes) or trace(not)

    I tried to make this condition but I couldn’t

    can any body help me please ????

  • ?????? ??????

    Sorry, for stupid question ……..How do I Get URL into Flash

  • study in UK/USA/AUS

    similar problem of endless loop. need to look into the issue

  • jason

    how did you get the example to work in this page? How did you call the .js? Did you embed it in the template file or how in the page?

  • Adrian

    Hi guys,
    I use this:

    var current_domain;
    function geturlhttp()
    current_domain =“window.location.href.toString”);

    and in embed code: allowscriptaccess=”always”
    and works fine.

  • DonJhonson3rd

    Excelente!!! muchas gracias

  • Jocuri

    Thanks, Great post!

  • Pingback: Access the html page URL and swf path from flash with as3 | circlecube()

  • jocuri

    thank for this post!

  • Sherryl Askin

    Thankyou for helping out, wonderful information.


    thanks. Especially for source documents :)

  • Matt

    Worked! Thanks!

  • Pingback: Flash 如何抓取目前網頁的網址 | Tsung's Blog()

  • to learn more

    Oh my goodness! Awesome article dude! Many thanks, However I am having issues with your
    RSS. I don’t understand why I cannot join it. Is there anybody else getting similar RSS issues? Anybody who knows the answer will you kindly respond? Thanx!!

  • Recent Posts


    What Is Code? If You Don’t Know, You Need to Read This

    Here’s a great read that may take a while but is a nice trip through programming history through the lens of one who’s been there. The world belongs to people who code. Those who don’t understand will be left behind. Source: What Is Code? If You Don’t Know, You Need to Read This Tweet

    Adactio: Journal—Instantiation

    There needs to be a cultural change in how we approach building for the web. Yes, some of the tools we choose are part of the problem, but the bigger problem is that performance still isn’t being recognised as the most important factor in how people feel about websites (and by extension, the web). This […]

    human_time_diff « WordPress Codex

    Determines the difference between two timestamps.The difference is returned in a human readable format such as “1 hour”, “5 mins”, “2 days”. Source: Function Reference/human time diff « WordPress Codex Used this little known, but nice gem today. It’s a core WordPress function that gives human relative time from any other datetime in “U” format. […]


    Cyclomatic Complexity: Logic in CSS – CSS Wizardry – CSS, OOCSS, front-end architecture, performance and more, by Harry Roberts

    I recently hit upon a way of thinking that made me realise that CSS does include logic, and the fact that it’s rarely viewed as such is probably also why we end up with such poor CSS at all.I found myself explaining compound selectors to a client as being made up of the subject—the thing […]


    Add Links to Twitter Mentions, Hashtags, and URLs with PHP

    This gem helped me out today as I was working with the Twitter API to create a custom WordPress widget. I was about to do exactly this and was not looking forward to it, luckily I did a quick search and found this webtips post. Thanks! If you’re using the Twitter v1.1 API to fetch […]

    Online regex tester and debugger: JavaScript, Python, PHP, and PCRE

    Stumbled on a great regex tool today. An explanation of your regex will be automatically generated as you type. Detailed match information will be displayed here automatically. via Online regex tester and debugger: JavaScript, Python, PHP, and PCRE. Tweet