import flash.external.ExternalInterface;
var pageURL:String = ExternalInterface.call('window.location.href.toString');
The above code seems to work on Firefox, but when I try it with Chrome or IE it doesn't work (it causes an Error and stops the swf's execution).
Any hint?
ExternalInterface
works in recent versions of all major browsers. The first thing you should do is wrap that call in a check to see if it is currently available:
if(ExternalInterface.available)
{
ExternalInterface.call('window.location.href.toString');
}
The problem with Chrome and IE may be the call to window.location.href. Your best bet is to put this into a JS function, and then call that function from AS, like so:
//JS:
function reportHref(){
return window.location.href.toString();
// I'm not sure this is good cross-browser JS.
// If it isn't, you can at least test it directly in the browser
// and get a javascript error that you can work on.
}
//AS:
var result:String = "";
if(ExternalInterface.available)
{
result = ExternalInterface.call("reportHref");
}
else
{
result = "External Interface unavailable";
}
trace(result);
Also, make sure the function you are trying to call is already present in the DOM before you try to call it - if you add your SWF before you add the script, and make the call to ExternalInterface
immediately, then it will fail because reportHref
doesn't exist yet.
And, finally, it's possible that a call from within the SWF to the window.location
object might fail due to sandboxing, which won't be the case if you make the call from a JS funciton in the page.
The docs on ExternalInterface
are pretty comprehensive, with good examples: