| Register
Friday, May 09, 2008   

The External Interface API - Learn by Coding

Created By  Chris Bizzell, at  10/26/2005 - 16 comments.

Click to view this author's website.

The new API is easy to implement and tremendously powerful. In this article I will
demonstrate just how easy it is to integrate into your projects.

View working example.

Download Source Files

The External Interface API is supported by the following platforms and
browsers:


In this example, we are going to use Javascript to load images into a Flash
movie as they are selected from an HTML form.

The Flash Movie


1. Open Flash 8 and create a new movie named image_ei.fla.


2. Drag a loader component onto the stage and name it loader.


3. Select the first frame and add the following code:

This code performs the following task:

a. import the classes needed to use the External Interface API
create a function that you can reference in the Javascript
that we will add to our HTML page

b. We will use the addCallBack method of the External Interface API
to trigger an action in our Flash movie once the HTML form is submitted

4. Publish your movie as a swf and HTML - we sill modify the HTML page next


The HTML Page


1. Open your page and add the following Javascript code in the header


There are two Javascript functions here. The first is used to get the name
of you embedded flash movie. If you look at the name and id attributes of
your Flash Movie, you will see the name - image_ei.

The next function - makeCall - calls the loadImage function we created inside
our Flash movie and passes in one argument; the value from our form that we
will create now.

2. In the body of your document add the following code:



This is the form that triggers our makeCall function and passes the value
of the select box to our Flash movie resulting in an image loading into our loader
component. Notice that the action attribute for this form calls our Javascript function
makeCall.




3. Save your file and upload it to your server for testing.
View working example.

If you test this example on your local machine, you may get a security warning that
your swf is trying to communicate with your web page. This is eliminated if you run the page
on a web server.


Need Professional Help For Your ActionScript Project?
ActionScript.com Consulting Services provide top quality professional ActionScript consulting to businesses around the globe. If you have a professional project in need to world-class talent, tell us about your project by requesting a quote today.

Reader Comments

  1. Carlos Rovira  Replied:
    ( 10/27/2005 At 12:41 PM)

    Nice example Chris,
    Thanks for sharing :)

  2. Anthony Graddy  Replied:
    ( 10/27/2005 At 11:43 PM)

    I think it is great that Flash is continually given more power to communicate with its surroundings, but whenever I see or hear about AJAX/Flash it justs gets me wondering if Macromedia is just trying to hop onto the latest web trend instead of promoting the power of its own products. Maybe I'm wrong, but all of my experience with Flash seems to indicate that it is just as powerful if not more powerful than an AJAX solution. If I am correct in that assessment, then why in the world would you want to go through all of the trouble of creating an extra layer of complexity by combining AJAX and Flash. I realize that in a few situations it is very beneficial for Flash to communicate with the page that it is on, but in most situations, I would think that an AJAX/Flash solution would simply be more work than is needed for similar functionality. Sorry for the rant, but I guess I just don't completely understand all of the hype around an AJAX/Flash solution.

  3. cvb  Replied:
    ( 10/28/2005 At 4:09 PM)

    I agree with you to a large degree Anthony. A lot of the hype that AJAX is getting seems to relate to things you could do years ago with a Flash application.
    What interested me in this example is the ability to
    integrate say a dynamic listbox generated by ASP, Cold Fusion, PHP etc with a Flash movie embedded on the same page. This makes your Flash movie dynamic without the use of Flash Remoting or loadVars for example.
    I do think that this type of AJAX/Flash integration is a good thing in that it gives the developer yet another tool be it for web development, Flash Projector files or whatever.

  4. Simon Davies  Replied:
    ( 10/28/2005 At 10:28 PM)

    Just a quick point, ExternalInterface has more use to it than just jumping on everyones favourite 4 letter acronym.
    My company produces CD-ROMs, many containing interactive flash navigation. ExternalInterface makes life soooo miuch easier and is so much better than dodgy fscommands.
    ExternalInterface can be accessed by any language that is being a container to the Flash ActiveX control, whether it is JavaScript, C#, C++, Authorware, Director etc.
    I don't think Macromedia had AJAX in my mind when they decided to update the non-consistent fscommand method of communicating with the outside world. Why would they use AJAX with Flash? It can already communicate with db's asynchronously via whatever web server is running.

  5. Sam  Replied:
    ( 11/10/2005 At 9:11 PM)

    Interesting!
    In the same way, does somebody know if we can make something similar with a db? Is there a way to link images to a database and load it dynamically into a flash apps?
    Thanks,

  6. Marcelo Ferreira  Replied:
    ( 11/14/2005 At 6:40 AM)

    Simon Davies is right: the main purpose of ExternalInterface is to have a standard and uniform way of communicating with your Flash container, for whatever reason you have to. Anybody who tried to make Flash fscommand or analogous code work correctly with PCs and MACs will recognize the benefit.

  7. shirley  Replied:
    ( 11/24/2005 At 5:47 PM)

    I have been posted on this issue (and I have searched) on other forums!
    May I ask you Chris if the External Interface Class works with incorporated flash.movies only?
    Thanks Shirley
    (The View working example doesnt work on my computer!_

  8. mark  Replied:
    ( 11/28/2005 At 12:39 AM)

    hey why does not the code work on my machine

  9. Ilayaraja  Replied:
    ( 12/5/2005 At 3:42 PM)

    hi,
    i am searched the tutorial for to write user interactive text a external and to retrive in it. Any body plz help this to open a external file and to save a external file thro flash

  10. eirc  Replied:
    ( 12/15/2005 At 11:54 PM)

    Hi,
    Are there any samples out there that demostrate how to integrate between flash and c++ app?

  11. Rutger Buijzen  Replied:
    ( 12/19/2005 At 5:41 PM)

    Hi,
    I'm using the standard flash player detection script that writes the swf embed code using javascript document.write. Is it possible that the thisMovie function doesn't function when the swf embed code is placed in the HTML this way?
    On another script i placed the swf embed code directly in the html an everything works fine.
    Thanks in advance

  12. kusha  Replied:
    ( 12/24/2005 At 2:24 PM)

    hi sir
    iam an flash learner.
    your methord is very good
    but i have problem in loding "n" number of of images in dynamicaly created movie clips, and both the images and the images name to be loaded should be from out side.
    thanks
    kusha

  13. dan marks  Replied:
    ( 1/4/2006 At 4:49 PM)

    we are using externalInterface with c# while we can get messages back and forth, we are having problems when we make a class for the communications is there a scope issue when you make a class for externalInterface functionality? it doesnt seem to like to communicate with other objects or raise events

  14. Greg McGee  Replied:
    ( 10/17/2006 At 7:18 PM)

    This looks like it should be a good example. However, it doesn't work at your link, nor can I make it work when I attempted it according to the instructions. http://www.mcgeecommerce.com/workfiles/image_ei.html

    I added a trace in the text field you see, to see if an image is loaded, and it isn't. There's probably something basic I'm not doing. Can you spot it?

  15. cb  Replied:
    ( 10/27/2006 At 10:50 AM)

    It works for me in Firefox -

  16. Trish McMillan  Replied:
    ( 10/11/2007 At 1:17 PM)

    I built this video player using the same methods, (still in development) basically, but now I need to figure out how to attach the video selection to the url (like youtube does).

    The goal is to be able to email someone a url that will open with this player loaded with the correct video.

    Any ideas?

Login to post your comments. If you do not have an account with us please Register.
Copyright 2005 by ActionScript, Inc.   |  Privacy Statement  |  Terms Of Use  |  ActionScript Client Extranet