Ran into a problem where ExternalInterface.call() in Flash only misbehaves in Internet Explorer.

The code is simple:

ExternalInterface.call("document.location.href.toString")

It works fine in Firefox, but Internet Explorer doesn’t return the value properly.

It doesn’t work when the swf is embedded with an html object tag. The cause is that the object had no id.

External API requirements and benefits

The External API is part of ActionScript. It provides a communication mechanism between ActionScript and the code running in the “external application” that hosts Flash Player as a container — typically a web browser or a standalone projector application. In ActionScript 3.0, the External API’s functionality is provided by the ExternalInterface class. In versions before Flash Player 8, the fscommand() action was used to communicate with the container application. Starting with Flash Player 8, the ExternalInterface class replaces fscommand(), so it’s recommended for all communication between JavaScript and ActionScript.

The ExternalInterface class is a subsystem that lets ActionScript and Flash Player communicate easily with JavaScript on an HTML page, or with a desktop application that embeds a Flash Player instance.

The ExternalInterface class can only be used in the following cases:

  • Any supported version of Internet Explorer for Windows (5.0 or later)
  • A container application, such as a desktop application, using an instance of the Flash Player ActiveX control
  • A browser that supports the NPRuntime interface, currently including:
    • Firefox 1.0 or later
    • Mozilla 1.7.5 or later
    • Netscape 8.0 or later
    • Safari 1.3 or later

In every other case (e.g. running in a standalone player), the ExternalInterface.available property returns false.

ActionScript can call JavaScript functions on an HTML page. Compared to fscommand(), the External API offers these improvements:

  • You can call any JavaScript function, not just the ones used with fscommand().
  • You can pass any number of arguments with any name — no longer limited to a single command plus a single string argument, so it’s far more flexible than fscommand().
  • You can pass different data types — Boolean, Number, String — no longer limited to String parameters.
  • You can receive the return value of the call, and it’s returned to ActionScript immediately as the result of the call.