ActionScript - How to get the current URL with Flash Actionscript

January 14th 2010

How do you get the current URL of a Flash page in Flash?

The question of how to get the current URL of a Flash movie has come my way a couple of times, so I thought I would post just how to do that. I have found a way that is simpler than most of the examples I have seen on the subject.

The Scenario..

You're working on the next coolest Flash application to hit the web and you have a need to find out what the current URL is. But how do you do that? Does Flash have a way to access this information?

The Solution..

I am not saying that I know everything about Flash, but I haven't found a way for Flash to directly access the current URL that the Flash file. But there's a way to have JavaScript do the work for you without ever having to write any JavaScript in the page.

The first thing we need to do is include the Flash external library. To do this we will use the import command. On the first frame of your Flash file add this line of Actionscript:

import flash.external.*;

This line tells Flash to include the external library which contains a function we will need to complete our task.

Next we will add a string variable to contain our current URL and call an external JavaScript function to snag our variable.

var currentURL:String = String("window.location.href.toString"));

We now have a variable with the current URL.

A couple of side notes for using this method. If you try to run this code in the Flash environment or as a Flash executable you will find your variable is set to "null". This is because both runtimes don't have access to JavaScript and there isn't a "URL".

Hope this helps.

Web Department Manager

About Us

WSI is a small business and a leading provider of custom programming and database solutions for government entities, Fortune 1000 companies, and emerging businesses. We are your custom development experts.