Getting current url in Flash using JavaScript with ExternalInterface and IE
I am trying to get the current url that Flash Player is included in. Not the URL of the .swf file, but the URL that the browser points to. So far I have 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 anyway.
The only thing I found on the internet is to put the id tag in the tag.
So I am trying to figure out if and / or how can I:
Somehow make a call using ExternalInterface in IE and other browsers to get me back the current url.
OR
Put id = "PA" attribute on the tag and read the AS3 tag and pull it as a string, without using JavaScript
My limitation is that I can ONLY add a tag to the HTML and cannot add any JavaScript functionality. This must be strictly done in AS3.
Anyway, I need to know what URL I am on. Any help is appreciated.
source to share
You need a few things to make it work in IE. First ActionScript:
var domain:String = ExternalInterface.call('function () { return window.location.href; }');
Second, you need valid classid and id attributes on the tag <object>
:
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="myplayer_123123" ...>
If you don't put these attributes in, ExternalInterface.call always returns null in IE6 / 7/8, but works as expected in firefox.
Third, you need to set the allowScriptAccess parameter to "always" to enable the use of ExternalInterface.
<param name='allowScriptAccess' value='always'/>
..
<embed allowscriptaccess='always' ...>
.....
source to share
Just a suggestion:
Probably because IE decided for some reason that window.location.href could be used as a function . It's asinine, but this is Microsoft for you.
Have you tried ExternalInterface.call ("String", "window.location.href")? This will be my next guess.
source to share
Have you thought about how to achieve what you want without an external challenge.
var domain:String = loaderInfo.loaderURL;
trace(domain.substr(0, domain.indexOf("/", 8))); //Searches for first instance of "/" after the 8th character.
Above, we track the base domain using indexOf to substitute the full path to swf. We look for the first "/" after the 8th character to return the endpoint of the substring. The reason we go in 8 characters is to allow http: // and https: //; we want him not to see those first "/". I tested this and it worked great.
There is nothing wrong with externalInterface calls, but I try to save them when needed.
source to share