Home Contact RSS

Get current url to Flash swf using an External Interface call

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...

Overview:

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 ExternalInterface.call requires a function rather than only reading a static property.

Steps:

  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 = ExternalInterface.call(”window.location.href.toString”);

  4. Call the function when/if needed.

Example:

With javascript: window.location.href or window.location.href.toString();
With actionscript: ExternalInterface.call(”window.location.href.toString”);
External Interface html Example
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
Actionscript:

  1. import flash.external.*;
  2. var urlPath;
  3.  
  4. function geturlhttp() {
  5. urlPath = ExternalInterface.call("window.location.href.toString");
  6. }
  7. geturlhttp();
  8. //Here I assign the url to a text box on the stage
  9. _root.urlText.text = urlPath;

Download:

ExternalInterface.zip

Tags: , , , , , , ,

Related posts

flashden banner

Gravatar

Jason said,

January 17, 2008 @ 5:27 pm

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).

http://bugs.adobe.com/jira/browse/ASC-3095

Gravatar

DB said,

January 24, 2008 @ 8:30 pm

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

Gravatar

Evan Mullins said,

January 25, 2008 @ 11:12 am

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?

Gravatar

agi said,

January 27, 2008 @ 3:24 am

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.

Gravatar

James said,

February 1, 2008 @ 11:54 am

@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…

Gravatar

Ed McManus said,

February 8, 2008 @ 10:41 pm

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 = ExternalInterface.call(’eval’, ‘window.location.href’);

Hope it helps!

Gravatar

Kirk Strobeck said,

March 4, 2008 @ 10:16 am

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 => http://flashURL.net

Gravatar

Evan Mullins said,

March 20, 2008 @ 11:30 pm

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

Gravatar

John T. Bailey said,

May 20, 2008 @ 10:36 am

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

[actionscript]
import flash.net.LocalConnection;
var lc:LocalConnection = new LocalConnection();
var domain:String = lc.domain;
trace(domain);
[/actionscript]

Gravatar

Sander van den Berg said,

May 29, 2008 @ 9:55 am

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.

Gravatar

nick said,

May 29, 2008 @ 9:29 pm

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

thanks

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

import flash.external.*;

function geturlhttp() {
_root.urlPath = ExternalInterface.call(”window.location.href.toString”);
}

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

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

Gravatar

Sohaib Ali said,

July 9, 2008 @ 11:33 pm


RSS feed for comments on this post · TrackBack URI

Leave a Comment