views:

1244

answers:

4

Hi, I'm trying to get the current URL that the Flash player is on. Not the URL of the .swf file, but the URL that the browser is pointing to. Thus far I've used:

var st:String = ExternalInterface.call("window.location.href");

Unfortunately this doesn't work in IE. From my research, I can see that it won't work with IE either way.

The only other thing I found around the Internet is putting an 'id' tag on the tag.

So I'm trying to find out if and/or how I can:

  1. Somehow make a call using the ExternalInterface in IE and other browsers to return to me the current URL.

    OR

  2. Slap an id="PA" attribute on the tag and have AS3 read that tag and pull it in as a String, without using JavaScript

My limitation is that I can ONLY add the tag to the HTML and cannot add any JavaScript functions. This has to be strictly done in AS3.

Either way, I need to know what URL I'm on. Any help is greatly appreciated.

A: 

Just a suggestion:

That is likely because IE has, for some reason, decided that window.location.href can be used as a function. It is asinine, but that is Microsoft for you.

Have you tried ExternalInterface.call( "String", "window.location.href" )? That would be my next guess.

Christopher W. Allen-Poole
Christopher, good point about how IE looks at href. I did try your approach, unfortunately ActionScript just returns "window.location.href.toString" as the string. :(
Tomaszewski
A: 
ExternalInterface.call('window.location.href.toString');
JStriedl
That was my first try. Firefox works great... but still no IE
Tomaszewski
That's odd, I tested it in IE6 and it worked fine. I don't have IE7 or IE8 available on this machine though, are you using IE7 or IE8?
JStriedl
yea, unfortunatly IE7/8 are my biggest concerns. Thanks though!
Tomaszewski
A: 

Have you given any thought to achieving what you want without an External Call.

var domain:String = loaderInfo.loaderURL;
trace(domain.substr(0, domain.indexOf("/", 8))); //Searches for first instance of "/" after the 8th character.

Above, we trace out the base domain using indexOf to make a sub-string from the full path to the swf. We search for the first instance of "/" after the 8th character to return the end point of the substring. The reason we go in 8 characters is to allow for http:// and https://; we need it not to see those first "/"'s. I tested this and it worked great.

There is nothing wrong with ExternalInterface calls, but I tend to save them for when required.

Brian Hodge hodgedev.com

Brian Hodge
Brian, indeed I have. Unfortunately there are multiple sub domains of varying sizes, like aaaaa-bbbb-ccc or abc.xyz.com... etc. So counting them doesn't work. To your credit, I have been using RegEx which has been helping. Thanks much.
Tomaszewski
What do you mean by "Counting them doesn't work."? No matter how nested the subdomains may be, the first "/" after http:// or https:// should be what comes after .com or .net etc. giving us the proper path.
Brian Hodge
+1  A: 

You need a couple of things in order to make it work in IE. First the ActionScript:

var domain:String = ExternalInterface.call('function () { return window.location.href; }');

Second, you need valid classid and id atributes in the <object> tag:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="myplayer_123123" ...>

If you don't put those attributes, ExternalInterface.call always returns null in IE6/7/8 but works as expected in firefox.

Third, you need to set the param allowScriptAccess to 'always', in order to enable the use of ExternalInterface.

<para name='allowScriptAccess' value='always'/>
..
<embed allowscriptaccess='always' ...>
Loïc Jeannin
i'll have to try this. Thanks!
Tomaszewski
Awesome! This works. Thanks.
fernyb