Before we jump right in and start using it, we need to make the necessary methods available on the timeline by importing the class like this:
The Call Method
The call method is a static method of the ExternalInterface object, which means we do not need an instance of the object to call it; we can call it directly on the object class, like this:
This method has two parameters:
That's the basic layout, so now let's build an example.
Now publish the movie and the HTML and place them on a server. When you test the file on the server, you will see that when you click the button, whatever is in the text field will appear in the alert. But, if you test locally, you will notice that when you click the button, nothing happens. This is a security feature of the Flash player, but there is a workaround.
To be able to run this file locally, you have to open the HTML in a text editor and change the allowScriptAccess parameter from "sameDomain" to "always" in both the object parameter and the embed attribute. After this is done, the file will work locally and the alert message will appear. The final version is available from the website, and it already has this change in place.
The addCallback Method
ExternalInterface.addCallback(functionID:String, instance:Object, functionName:Function);
As with the call method, this method is a static method and it has these three parameters:
First, we create the variable me to hold the reference to the SWF file; without it, we could not make calls to Flash functions. Then the function that will set the reference is created with a conditional that can tell the difference between Microsoft's Internet Explorer and other browsers, because the reference will need to be different for that browser. After that, the function makeCall is created, and this is where the Flash function is called. Finally, we create the function that is being called from Flash, which will call the makeCall function to send information back to Flash, but not before it receives the data back from the prompt.
In the body tag, add the attribute onload and set it equal to the getID function like this:
<body bgcolor="#ffffff" onload="getID('external2');">
Put the files up on the server to test, or again, change the allowScriptAccess parameter and attribute to test locally. When it is run, you should see something similar to Figure 20.8. And when you set the color and click OK (or press the Enter key), the square should change to that color.