"We have been very pleased with the competent, prompt and courteous service they have provided to us."

Stu Perlman
Monarch/USESI (New Jersey)

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(ExternalInterface.call("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

Iowa Headquarters
(MIDWEST)
103 E. 6th St., Suite 101
P.O. Box 726
Ames, Iowa 50010
Tel: (515) 239-9900
Fax: (515) 239-9800

Driving Directions to the Ames Iowa Headquarters
Florida Headquarters
(Southeast)
4200 Evans Ave., Suite 10
Fort Myers, Florida 33901
Tel: (239) 267-2749
Fax: (239) 267-9936
Caribbean & Latin America
(Puerto Rico)
Tel: (787) 303-0042
South Dakota - Sioux Falls
Tel: (605) 496-9580
Texas - Austin
Tel: (512) 593-1056
New Mexico - Albuquerque
Tel: (505) 289-1364
Iowa/Illinois - Quad Cities/Muscatine
Tel: (563) 316-2378
North Carolina - Asheville
Tel: (828) 278-2323
Want quick Answers?
During normal business hours you can reach us via our live chat program.  Try it, it's really cool!!
 
Partner Locations
Cedar Rapids, Iowa
 
Interested in joining our partner program?  Click Here to learn more about the Winning Solutions, Inc. Partner Program.